public static void OnPostprocessBuild(BuildTarget buildTarget, string path) { if (buildTarget != BuildTarget.iOS) { return; } // Check if we have the minimal iOS version set. bool hasMinOSVersion; try { var requiredVersion = new Version(10, 0); var currentVersion = new Version(PlayerSettings.iOS.targetOSVersionString); hasMinOSVersion = currentVersion >= requiredVersion; } catch (Exception) { hasMinOSVersion = false; } if (!hasMinOSVersion) { Debug.Log("UserNotifications framework is only available on iOS 10.0+, please make sure that you set a correct `Target minimum iOS Version` in Player Settings."); } var settings = NotificationSettingsManager.Initialize().iOSNotificationSettingsFlat; var needLocationFramework = (bool)settings.Find(i => i.Key == "UnityUseLocationNotificationTrigger").Value; var addPushNotificationCapability = (bool)settings.Find(i => i.Key == "UnityAddRemoteNotificationCapability").Value; var useReleaseAPSEnv = false; if (addPushNotificationCapability) { var useReleaseAPSEnvSetting = settings.Find(i => i.Key == "UnityUseAPSReleaseEnvironment"); if (useReleaseAPSEnvSetting != null) { useReleaseAPSEnv = (bool)useReleaseAPSEnvSetting.Value; } } PatchPBXProject(path, needLocationFramework, addPushNotificationCapability, useReleaseAPSEnv); PatchPlist(path, settings, addPushNotificationCapability); PatchPreprocessor(path, needLocationFramework, addPushNotificationCapability); }
public static void OnPostprocessBuild(BuildTarget buildTarget, string path) { if (buildTarget == BuildTarget.iOS) { var projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj"; var proj = new PBXProject(); proj.ReadFromString(File.ReadAllText(projPath)); string mainTarget; string unityFrameworkTarget; var unityMainTargetGuidMethod = proj.GetType().GetMethod("GetUnityMainTargetGuid"); var unityFrameworkTargetGuidMethod = proj.GetType().GetMethod("GetUnityFrameworkTargetGuid"); if (unityMainTargetGuidMethod != null && unityFrameworkTargetGuidMethod != null) { mainTarget = (string)unityMainTargetGuidMethod.Invoke(proj, null); unityFrameworkTarget = (string)unityFrameworkTargetGuidMethod.Invoke(proj, null); } else { mainTarget = proj.TargetGuidByName("Unity-iPhone"); unityFrameworkTarget = mainTarget; } var settings = NotificationSettingsManager.Initialize().iOSNotificationSettingsFlat; var addPushNotificationCapability = (bool)settings .Find(i => i.key == "UnityAddRemoteNotificationCapability").val == true;; var needLocationFramework = (bool)settings .Find(i => i.key == "UnityUseLocationNotificationTrigger").val == true;; proj.AddFrameworkToProject(unityFrameworkTarget, "UserNotifications.framework", true); if (needLocationFramework) { proj.AddFrameworkToProject(unityFrameworkTarget, "CoreLocation.framework", false); } File.WriteAllText(projPath, proj.WriteToString()); if (addPushNotificationCapability) { var useReleaseAPSEnvSetting = settings.Find(i => i.key == "UnityUseAPSReleaseEnvironment"); var useReleaseAPSEnv = false; if (useReleaseAPSEnvSetting != null) { useReleaseAPSEnv = (bool)useReleaseAPSEnvSetting.val; } var entitlementsFileName = proj.GetBuildPropertyForAnyConfig(mainTarget, "CODE_SIGN_ENTITLEMENTS"); if (entitlementsFileName == null) { var bundleIdentifier = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS); entitlementsFileName = string.Format("{0}.entitlements", bundleIdentifier.Substring(bundleIdentifier.LastIndexOf(".") + 1)); } var pbxPath = PBXProject.GetPBXProjectPath(path); var capManager = new ProjectCapabilityManager(pbxPath, entitlementsFileName, "Unity-iPhone"); capManager.AddPushNotifications(!useReleaseAPSEnv); capManager.WriteToFile(); } // Get plist var plistPath = path + "/Info.plist"; var plist = new PlistDocument(); plist.ReadFromString(File.ReadAllText(plistPath)); // Get root var rootDict = plist.root; var requiredVersion = new Version(8, 0); bool hasMinOSVersion; try { var currentVersion = new Version(PlayerSettings.iOS.targetOSVersionString); hasMinOSVersion = currentVersion >= requiredVersion; } catch (Exception) { hasMinOSVersion = false; } if (!hasMinOSVersion) { Debug.Log("UserNotifications are only available on iOSSettings 10 and above, please make sure that you set a correct `Target minimum iOSSettings Version` in Player Settings."); } foreach (var setting in settings) { if (setting.val.GetType() == typeof(bool)) { rootDict.SetBoolean(setting.key, (bool)setting.val); } else if (setting.val.GetType() == typeof(PresentationOption) || setting.val.GetType() == typeof(AuthorizationOption)) { rootDict.SetInteger(setting.key, (int)setting.val); } } if (addPushNotificationCapability) { PlistElementArray currentBacgkgroundModes = (PlistElementArray)rootDict["UIBackgroundModes"]; if (currentBacgkgroundModes == null) { currentBacgkgroundModes = rootDict.CreateArray("UIBackgroundModes"); } currentBacgkgroundModes.AddString("remote-notification"); } // Write to file File.WriteAllText(plistPath, plist.WriteToString()); //Get Preprocessor.h var preprocessorPath = path + "/Classes/Preprocessor.h"; var preprocessor = File.ReadAllText(preprocessorPath); if (needLocationFramework) { if (preprocessor.Contains("UNITY_USES_LOCATION")) { preprocessor = preprocessor.Replace("UNITY_USES_LOCATION 0", "UNITY_USES_LOCATION 1"); } } preprocessor = preprocessor.Replace("UNITY_USES_REMOTE_NOTIFICATIONS 0", "UNITY_USES_REMOTE_NOTIFICATIONS 1"); File.WriteAllText(preprocessorPath, preprocessor); } }
public void ResetSettings() { NotificationSettingsManager.DeleteSettings(); }