Advertisement
Guest User

Untitled

a guest
Oct 30th, 2019
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.79 KB | None | 0 0
  1. #define ADD_APP_GROUP
  2. //remove to prevent the addition of the app group
  3.  
  4. #if UNITY_5_4_OR_NEWER && UNITY_IPHONE && UNITY_EDITOR
  5.  
  6.    using System.IO;
  7.    using System;
  8.    using UnityEngine;
  9.    using UnityEditor;
  10.    using UnityEditor.Callbacks;
  11.    using UnityEditor.iOS.Xcode;
  12.    using System.Text;
  13.  
  14.    #if UNITY_2017_2_OR_NEWER
  15.       using UnityEditor.iOS.Xcode.Extensions;
  16.    #endif
  17.  
  18.    /*
  19.          Adds required frameworks to the iOS project, and adds the OneSignalNotificationServiceExtension
  20.          Also handles making sure both targets (app and extension service) have the correct dependencies
  21.       */
  22.  
  23.    public class BuildPostProcessor
  24.    {
  25.       [PostProcessBuildAttribute(1)]
  26.       public static void OnPostProcessBuild(BuildTarget target, string path)
  27.       {
  28.          var separator = Path.DirectorySeparatorChar;
  29.          
  30.          string projectPath = PBXProject.GetPBXProjectPath(path);
  31.          PBXProject project = new PBXProject();
  32.  
  33.          project.ReadFromString(File.ReadAllText(projectPath));
  34.          string targetName = PBXProject.GetUnityTargetName();
  35.          string targetGUID = project.TargetGuidByName(targetName);
  36.  
  37.          var frameworks = new string[] {"NotificationCenter.framework", "UserNotifications.framework", "UIKit.framework", "SystemConfiguration.framework", "CoreGraphics.framework", "WebKit.framework"};
  38.  
  39.          foreach (string framework in frameworks) {
  40.             project.AddFrameworkToProject (targetGUID, framework, false);
  41.          }
  42.        
  43.          #if UNITY_2017_2_OR_NEWER && !UNITY_CLOUD_BUILD
  44.            
  45.             var platformsLocation = "Assets" + separator + "OneSignal" + separator + "Platforms" + separator;
  46.             var extensionTargetName = "OneSignalNotificationServiceExtension";
  47.             var pathToNotificationService = path + separator + extensionTargetName;
  48.  
  49.             var notificationServicePlistPath = pathToNotificationService + separator + "Info.plist";
  50.      
  51.             //if this is a rebuild, we've already added the extension service, no need to run this script a second time
  52.             if (File.Exists(notificationServicePlistPath))
  53.                return;
  54.      
  55.             Directory.CreateDirectory(pathToNotificationService);
  56.  
  57.             PlistDocument notificationServicePlist = new PlistDocument();
  58.             notificationServicePlist.ReadFromFile (platformsLocation + "iOS" + separator + "Info.plist");
  59.             notificationServicePlist.root.SetString ("CFBundleShortVersionString", PlayerSettings.bundleVersion);
  60.             notificationServicePlist.root.SetString ("CFBundleVersion", PlayerSettings.iOS.buildNumber.ToString ());
  61.  
  62.             var notificationServiceTarget = PBXProjectExtensions.AddAppExtension (project, targetGUID, extensionTargetName, PlayerSettings.GetApplicationIdentifier (BuildTargetGroup.iOS) + "." + extensionTargetName, notificationServicePlistPath);
  63.  
  64.             var sourceDestination = extensionTargetName + "/NotificationService";
  65.  
  66.             project.AddFileToBuild (notificationServiceTarget, project.AddFile (sourceDestination + ".h", sourceDestination + ".h", PBXSourceTree.Source));
  67.             project.AddFileToBuild (notificationServiceTarget, project.AddFile (sourceDestination + ".m", sourceDestination + ".m", PBXSourceTree.Source));
  68.  
  69.             foreach (string framework in frameworks) {
  70.                project.AddFrameworkToProject (notificationServiceTarget, framework, true);
  71.             }
  72.  
  73.             //makes it so that the extension target is Universal (not just iPhone) and has an iOS 10 deployment target
  74.             project.SetBuildProperty(notificationServiceTarget, "TARGETED_DEVICE_FAMILY", "1,2");
  75.             project.SetBuildProperty(notificationServiceTarget, "IPHONEOS_DEPLOYMENT_TARGET", "10.0");
  76.  
  77.             project.SetBuildProperty (notificationServiceTarget, "ARCHS", "$(ARCHS_STANDARD)");
  78.             project.SetBuildProperty (notificationServiceTarget, "DEVELOPMENT_TEAM", PlayerSettings.iOS.appleDeveloperTeamID);
  79.  
  80.             notificationServicePlist.WriteToFile (notificationServicePlistPath);
  81.      
  82.             foreach (string type in new string[] { "m", "h" })
  83.                if (!File.Exists(path + separator + sourceDestination + "." + type))
  84.                   FileUtil.CopyFileOrDirectory(platformsLocation + "iOS" + separator + "NotificationService." + type, path + separator + sourceDestination + "." + type);
  85.  
  86.             project.WriteToFile (projectPath);
  87.  
  88.             //add libOneSignal.a to the OneSignalNotificationServiceExtension target
  89.             string contents = File.ReadAllText(projectPath);
  90.  
  91.             //this method only modifies the PBXProject string passed in (contents).
  92.             //after this method finishes, we must write the contents string to disk
  93.             InsertStaticFrameworkIntoTargetBuildPhaseFrameworks("libOneSignal", "CD84C25F20742FAB0035D524", notificationServiceTarget, ref contents, project);
  94.             File.WriteAllText(projectPath, contents);
  95.  
  96.          #else
  97.             project.WriteToFile (projectPath);
  98.  
  99.             string contents = File.ReadAllText(projectPath);
  100.          #endif
  101.  
  102.          // enable the Notifications capability in the main app target
  103.          project.ReadFromString(contents);
  104.          var entitlementPath = path + separator + targetName + separator + targetName + ".entitlements";
  105.  
  106.          PlistDocument entitlements = new PlistDocument();
  107.  
  108.          if (File.Exists(entitlementPath))
  109.             entitlements.ReadFromFile(entitlementPath);
  110.          
  111.          if (entitlements.root["aps-environment"] == null)
  112.             entitlements.root.SetString("aps-environment", "development");
  113.  
  114.          #if !UNITY_CLOUD_BUILD && ADD_APP_GROUP
  115.             if (entitlements.root["com.apple.security.application-groups"] == null) {
  116.                var groups = entitlements.root.CreateArray("com.apple.security.application-groups");
  117.                groups.AddString("group." + PlayerSettings.applicationIdentifier + ".onesignal");
  118.             }
  119.          #endif
  120.  
  121.          entitlements.WriteToFile(entitlementPath);
  122.  
  123.          // Copy the entitlement file to the xcode project
  124.          var entitlementFileName = Path.GetFileName(entitlementPath);
  125.          var unityTarget = PBXProject.GetUnityTargetName();
  126.          var relativeDestination = unityTarget + "/" + entitlementFileName;
  127.  
  128.          // Add the pbx configs to include the entitlements files on the project
  129.          project.AddFile(relativeDestination, entitlementFileName);
  130.          project.AddBuildProperty(targetGUID, "CODE_SIGN_ENTITLEMENTS", relativeDestination);
  131.  
  132.          // Add push notifications as a capability on the target
  133.          project.AddBuildProperty(targetGUID, "SystemCapabilities", "{com.apple.Push = {enabled = 1;};}");
  134.  
  135.          // Add OneSignal lib paths
  136.          project.AddBuildProperty(notificationServiceTarget, "LIBRARY_SEARCH_PATHS", "$(PROJECT_DIR)/Libraries/OneSignal/Platforms/iOS");
  137.          project.AddBuildProperty(targetGUID, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)/Libraries/**");
  138.  
  139.          File.WriteAllText(projectPath, project.WriteToString());
  140.       }
  141.  
  142.       #if UNITY_2017_2_OR_NEWER && !UNITY_CLOUD_BUILD
  143.          
  144.          // This function takes a static framework that is already linked to a different target in the project and links it to the specified target
  145.          public static void InsertStaticFrameworkIntoTargetBuildPhaseFrameworks(string staticFrameworkName, string frameworkGuid, string target, ref string contents, PBXProject project) {
  146.             //in order to find the fileRef, find the PBXBuildFile objects section of the PBXProject
  147.             string splitString = " /* " + staticFrameworkName + ".a in Frameworks */ = {isa = PBXBuildFile; fileRef = ";
  148.             string[] splitComponents = contents.Split(new string[] {splitString}, StringSplitOptions.None);
  149.  
  150.             if (splitComponents.Length < 2) {
  151.                Debug.LogError ("(error 1) OneSignal's Build Post Processor has encountered an error while attempting to add the Notification Extension Service to your project. Please create an issue on our OneSignal-Unity-SDK repo on GitHub.");
  152.                return;
  153.             }
  154.  
  155.             string afterSplit = splitComponents[1];
  156.  
  157.             //to get the fileRef of the static framework, read the last 24 characters of the beforeSplit string
  158.             StringBuilder fileRefBuilder = new StringBuilder();
  159.  
  160.             for (int i = 0; i < 24; i++) {
  161.                fileRefBuilder.Append(afterSplit[i]);
  162.             }
  163.  
  164.             string fileRef = fileRefBuilder.ToString();
  165.  
  166.             project.AddFileToBuild(target, fileRef);
  167.  
  168.             //add the framework as an additional object in PBXBuildFile objects
  169.             contents = contents.Replace("; fileRef = " + fileRef + " /* " + staticFrameworkName + ".a */; };", "; fileRef = " + fileRef + " /* " + staticFrameworkName + ".a */; };\n\t\t" + frameworkGuid + " /* " + staticFrameworkName + ".a in Frameworks */ = {isa = PBXBuildFile; fileRef = " + fileRef + " /* " + staticFrameworkName + ".a */; };");
  170.  
  171.             //find the build phase ID number
  172.             string targetBuildPhaseId = project.GetFrameworksBuildPhaseByTarget(target);
  173.             string[] components = contents.Split(new string[] { targetBuildPhaseId + " /* Frameworks */ = {\n\t\t\tisa = PBXFrameworksBuildPhase;\n\t\t\tbuildActionMask = " }, StringSplitOptions.None);
  174.  
  175.             if (components.Length < 2) {
  176.                Debug.LogError("(error 2) OneSignal's Build Post Processor has encountered an error while attempting to add the Notification Extension Service to your project. Please create an issue on our OneSignal-Unity-SDK repo on GitHub.");
  177.                return;
  178.             }
  179.  
  180.             string buildPhaseString = components[1];
  181.  
  182.             StringBuilder replacer = new StringBuilder ();
  183.  
  184.             for (int i = 0; i < buildPhaseString.Length; i++) {
  185.                char seq = buildPhaseString [i];
  186.  
  187.                if (char.IsNumber (seq)) {
  188.                   replacer.Append (seq);
  189.                } else {
  190.                   break;
  191.                }
  192.             }
  193.  
  194.             // insert the framework into the PBXFrameworksBuildPhase
  195.             string beginString = targetBuildPhaseId + " /* Frameworks */ = {\n\t\t\tisa = PBXFrameworksBuildPhase;\n\t\t\tbuildActionMask = " + replacer.ToString() + ";\n\t\t\tfiles = (";
  196.             contents = contents.Replace(beginString, beginString + "\n" + "\t\t\t\t" + frameworkGuid + " /* " + staticFrameworkName + ".a in Frameworks */,");
  197.  
  198.             //add library search paths to add build configurations of the target
  199.             contents = contents.Replace ("PRODUCT_BUNDLE_IDENTIFIER = ", "LIBRARY_SEARCH_PATHS = (\n\t\t\t\t\t\"$(inherited)\",\n\t\t\t\t\t\"$(PROJECT_DIR)/Libraries/OneSignal/Platforms/iOS\",\n\t\t\t\t);\nPRODUCT_BUNDLE_IDENTIFIER = ");
  200.          }
  201.  
  202.       #endif
  203.    }
  204.  
  205. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement