Skip to content

Commit

Permalink
Moved iOS build post-processor settings to 'Project Settings/yasirkul…
Browse files Browse the repository at this point in the history
…a/Native Gallery'
  • Loading branch information
yasirkula committed Jul 5, 2021
1 parent 04262e9 commit 29566b8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ There are two ways to set up the plugin on iOS:

**a. Automated Setup for iOS**

- *(optional)* change the value of **PHOTO_LIBRARY_USAGE_DESCRIPTION** and **PHOTO_LIBRARY_ADDITIONS_USAGE_DESCRIPTION** in *Plugins/NativeGallery/Editor/NGPostProcessBuild.cs*
- *(Unity 2017.4 or earlier)* if your minimum *Deployment Target* (iOS Version) is at least 8.0, set the value of **MINIMUM_TARGET_8_OR_ABOVE** to *true* in *NGPostProcessBuild.cs*
- *(optional)* change the values of **Photo Library Usage Description** and **Photo Library Additions Usage Description** at *Project Settings/yasirkula/Native Gallery*
- *(Unity 2017.4 or earlier)* if your minimum *Deployment Target* (iOS Version) is at least 8.0, set the value of **Deployment Target Is 8.0 Or Above** to *true* at *Project Settings/yasirkula/Native Gallery*

**b. Manual Setup for iOS**

Expand Down
109 changes: 87 additions & 22 deletions Plugins/NativeGallery/Editor/NGPostProcessBuild.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,98 @@
#if UNITY_IOS
using System.IO;
using System.IO;
using UnityEditor;
using UnityEngine;
#if UNITY_IOS
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
#endif

namespace NativeGalleryNamespace
{
public class NGPostProcessBuild
[System.Serializable]
public class Settings
{
private const bool ENABLED = true;
private const string SAVE_PATH = "ProjectSettings/NativeGallery.json";

public bool AutomatedSetup = true;
#if !UNITY_2018_1_OR_NEWER
public bool MinimumiOSTarget8OrAbove = false;
#endif
public string PhotoLibraryUsageDescription = "The app requires access to Photos to interact with it.";
public string PhotoLibraryAdditionsUsageDescription = "The app requires access to Photos to save media to it.";
public bool DontAskLimitedPhotosPermissionAutomaticallyOnIos14 = true; // See: https://mackuba.eu/2020/07/07/photo-library-changes-ios-14/

private static Settings m_instance = null;
public static Settings Instance
{
get
{
if( m_instance == null )
{
try
{
if( File.Exists( SAVE_PATH ) )
m_instance = JsonUtility.FromJson<Settings>( File.ReadAllText( SAVE_PATH ) );
else
m_instance = new Settings();
}
catch( System.Exception e )
{
Debug.LogException( e );
m_instance = new Settings();
}
}

return m_instance;
}
}

public void Save()
{
File.WriteAllText( SAVE_PATH, JsonUtility.ToJson( this, true ) );
}

#if UNITY_2018_3_OR_NEWER
[SettingsProvider]
public static SettingsProvider CreatePreferencesGUI()
{
return new SettingsProvider( "Project/yasirkula/Native Gallery", SettingsScope.Project )
{
guiHandler = ( searchContext ) => PreferencesGUI(),
keywords = new System.Collections.Generic.HashSet<string>() { "Native", "Gallery", "Android", "iOS" }
};
}
#endif

#if !UNITY_2018_3_OR_NEWER
[PreferenceItem( "Native Gallery" )]
#endif
public static void PreferencesGUI()
{
EditorGUI.BeginChangeCheck();

Instance.AutomatedSetup = EditorGUILayout.Toggle( "Automated Setup", Instance.AutomatedSetup );

private const string PHOTO_LIBRARY_USAGE_DESCRIPTION = "The app requires access to Photos to interact with it.";
private const string PHOTO_LIBRARY_ADDITIONS_USAGE_DESCRIPTION = "The app requires access to Photos to save media to it.";
private const bool DONT_ASK_LIMITED_PHOTOS_PERMISSION_AUTOMATICALLY_ON_IOS14 = true; // See: https://mackuba.eu/2020/07/07/photo-library-changes-ios-14/
EditorGUI.BeginDisabledGroup( !Instance.AutomatedSetup );
#if !UNITY_2018_1_OR_NEWER
private const bool MINIMUM_TARGET_8_OR_ABOVE = false;
Instance.MinimumiOSTarget8OrAbove = EditorGUILayout.Toggle( "Deployment Target Is 8.0 Or Above", Instance.MinimumiOSTarget8OrAbove );
#endif
Instance.PhotoLibraryUsageDescription = EditorGUILayout.DelayedTextField( "Photo Library Usage Description", Instance.PhotoLibraryUsageDescription );
Instance.PhotoLibraryAdditionsUsageDescription = EditorGUILayout.DelayedTextField( "Photo Library Additions Usage Description", Instance.PhotoLibraryAdditionsUsageDescription );
Instance.DontAskLimitedPhotosPermissionAutomaticallyOnIos14 = EditorGUILayout.Toggle( new GUIContent( "Don't Ask Limited Photos Permission Automatically", "See: https://mackuba.eu/2020/07/07/photo-library-changes-ios-14/. It's recommended to keep this setting enabled" ), Instance.DontAskLimitedPhotosPermissionAutomaticallyOnIos14 );
EditorGUI.EndDisabledGroup();

if( EditorGUI.EndChangeCheck() )
Instance.Save();
}
}

public class NGPostProcessBuild
{
#if UNITY_IOS
#pragma warning disable 0162
[PostProcessBuild]
[PostProcessBuild( 1 )]
public static void OnPostprocessBuild( BuildTarget target, string buildPath )
{
if( !ENABLED )
if( !Settings.Instance.AutomatedSetup )
return;

if( target == BuildTarget.iOS )
Expand All @@ -43,24 +111,22 @@ public static void OnPostprocessBuild( BuildTarget target, string buildPath )

// Minimum supported iOS version on Unity 2018.1 and later is 8.0
#if !UNITY_2018_1_OR_NEWER
if( MINIMUM_TARGET_8_OR_ABOVE )
if( !Settings.Instance.MinimumiOSTarget8OrAbove )
{
#endif
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-weak_framework Photos" );
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-weak_framework PhotosUI" );
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework Photos" );
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework AssetsLibrary" );
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework MobileCoreServices" );
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework ImageIO" );
#if !UNITY_2018_1_OR_NEWER
}
else
#endif
{
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-weak_framework Photos" );
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-weak_framework PhotosUI" );
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework AssetsLibrary" );
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework Photos" );
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework MobileCoreServices" );
pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework ImageIO" );
}
#endif

pbxProject.RemoveFrameworkFromProject( targetGUID, "Photos.framework" );

Expand All @@ -70,15 +136,14 @@ public static void OnPostprocessBuild( BuildTarget target, string buildPath )
plist.ReadFromString( File.ReadAllText( plistPath ) );

PlistElementDict rootDict = plist.root;
rootDict.SetString( "NSPhotoLibraryUsageDescription", PHOTO_LIBRARY_USAGE_DESCRIPTION );
rootDict.SetString( "NSPhotoLibraryAddUsageDescription", PHOTO_LIBRARY_ADDITIONS_USAGE_DESCRIPTION );
if( DONT_ASK_LIMITED_PHOTOS_PERMISSION_AUTOMATICALLY_ON_IOS14 )
rootDict.SetString( "NSPhotoLibraryUsageDescription", Settings.Instance.PhotoLibraryUsageDescription );
rootDict.SetString( "NSPhotoLibraryAddUsageDescription", Settings.Instance.PhotoLibraryAdditionsUsageDescription );
if( Settings.Instance.DontAskLimitedPhotosPermissionAutomaticallyOnIos14 )
rootDict.SetBoolean( "PHPhotoLibraryPreventAutomaticLimitedAccessAlert", true );

File.WriteAllText( plistPath, plist.WriteToString() );
}
}
#pragma warning restore 0162
#endif
}
}
6 changes: 3 additions & 3 deletions Plugins/NativeGallery/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ for Android: set "Write Permission" to "External (SDCard)" in Player Settings
for iOS: there are two ways to set up the plugin on iOS:

a. Automated Setup for iOS
- (optional) change the values of PHOTO_LIBRARY_USAGE_DESCRIPTION and PHOTO_LIBRARY_ADDITIONS_USAGE_DESCRIPTION in Plugins/NativeGallery/Editor/NGPostProcessBuild.cs
- (Unity 2017.4 or earlier) if your minimum Deployment Target (iOS Version) is at least 8.0, set the value of MINIMUM_TARGET_8_OR_ABOVE to true in NGPostProcessBuild.cs
- (optional) change the values of 'Photo Library Usage Description' and 'Photo Library Additions Usage Description' at 'Project Settings/yasirkula/Native Gallery'
- (Unity 2017.4 or earlier) if your minimum Deployment Target (iOS Version) is at least 8.0, set the value of 'Deployment Target Is 8.0 Or Above' to true at 'Project Settings/yasirkula/Native Gallery'

b. Manual Setup for iOS
- set the value of ENABLED to false in NGPostProcessBuild.cs
- set the value of 'Automated Setup' to false at 'Project Settings/yasirkula/Native Gallery'
- build your project
- enter a Photo Library Usage Description to Info.plist in Xcode
- also enter a "Photo Library Additions Usage Description" to Info.plist in Xcode, if exists
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.nativegallery",
"displayName": "Native Gallery",
"version": "1.6.3",
"version": "1.6.4",
"documentationUrl": "https://github.com/yasirkula/UnityNativeGallery",
"changelogUrl": "https://github.com/yasirkula/UnityNativeGallery/releases",
"licensesUrl": "https://github.com/yasirkula/UnityNativeGallery/blob/master/LICENSE.txt",
Expand Down

0 comments on commit 29566b8

Please sign in to comment.