private static void AddCapability(MOBXCodeEditorModel xcodeModel, PBXProject xcodeProj, string xcodeTargetGuid, string xcodeTargetPath) { string projectPath = PBXProject.GetPBXProjectPath(xcodeTargetPath); if (xcodeModel.isOpenRestoreScene || xcodeModel.isHaveApple) { string entitlementsPath = xcodeModel.entitlementsPath; if (entitlementsPath == null || entitlementsPath == "" || !xcodeModel.entitlementsPath.Contains(".entitlements")) { string[] s = UnityEditor.PlayerSettings.applicationIdentifier.Split('.'); string productname = s[s.Length - 1]; entitlementsPath = "Unity-iPhone/" + productname + ".entitlements"; } ProjectCapabilityManager capManager = new ProjectCapabilityManager(projectPath, entitlementsPath, PBXProject.GetUnityTargetName()); if (xcodeModel.associatedDomains.Count > 0 || xcodeModel.isHaveApple) { string[] domains = new string[xcodeModel.associatedDomains.Count]; int index = 0; foreach (string domainStr in xcodeModel.associatedDomains) { domains[index] = domainStr; index++; } capManager.AddAssociatedDomains(domains); if (xcodeModel.isHaveApple && capManager.GetType().GetMethod("AddSignInWithApple") != null) { capManager.GetType().GetMethod("AddSignInWithApple").Invoke(capManager, null); } //推送 //capManager.AddPushNotifications(true); //内购 //capManager.AddInAppPurchase(); capManager.WriteToFile(); //Debug.Log("AddCapabilityAssociatedDomains"); //Debug.Log("xcodeTargetGuid:" + xcodeTargetGuid); //Debug.Log("xcodeTargetPath:" + xcodeTargetPath); //Debug.Log("projectPath:" + projectPath); //Debug.Log("GetUnityTargetName:" + PBXProject.GetUnityTargetName()); //Debug.Log("bundleIdentifier:" + UnityEditor.PlayerSettings.applicationIdentifier); //Debug.Log("productName:" + UnityEditor.PlayerSettings.productName); xcodeProj.AddCapability(xcodeTargetGuid, PBXCapabilityType.AssociatedDomains, xcodeTargetPath + "/" + xcodeModel.entitlementsPath, true); } } }
private static void AddNFCCapability(string path) { string projectPath = PBXProject.GetPBXProjectPath(path); //PBXProject project = new PBXProject(); //project.ReadFromFile(projectPath); String packageName = UnityEngine.Application.identifier; String name = packageName.Substring(packageName.LastIndexOf('.') + 1); String entitlementFileName = name + ".entitlements"; String entitlementPath = Path.Combine(path, entitlementFileName); ProjectCapabilityManager projectCapabilityManager = new ProjectCapabilityManager(projectPath, entitlementFileName, PBXProject.GetUnityTargetName()); PlistDocument entitlementDocument = AddNFCEntitlement(projectCapabilityManager); entitlementDocument.WriteToFile(entitlementPath); var projectInfo = projectCapabilityManager.GetType().GetField("project", BindingFlags.NonPublic | BindingFlags.Instance); PBXProject project = (PBXProject)projectInfo.GetValue(projectCapabilityManager); string target = project.TargetGuidByName(PBXProject.GetUnityTargetName()); var constructor = typeof(PBXCapabilityType).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(bool), typeof(string), typeof(bool) }, null); PBXCapabilityType nfcCapability = (PBXCapabilityType)constructor.Invoke(new object[] { "com.apple.NearFieldCommunicationTagReading", true, "", false }); project.AddCapability(target, nfcCapability, entitlementFileName); projectCapabilityManager.WriteToFile(); }
private static PlistDocument AddNFCEntitlement(ProjectCapabilityManager projectCapabilityManager) { MethodInfo getMethod = projectCapabilityManager.GetType().GetMethod("GetOrCreateEntitlementDoc", BindingFlags.NonPublic | BindingFlags.Instance); PlistDocument entitlementDoc = (PlistDocument)getMethod.Invoke(projectCapabilityManager, new object[] { }); PlistElementDict dictionary = entitlementDoc.root; PlistElementArray array = dictionary.CreateArray("com.apple.developer.nfc.readersession.formats"); array.values.Add(new PlistElementString("NDEF")); return(entitlementDoc); }
private static void AddCapability(MOBXCodeEditorModel xcodeModel, PBXProject xcodeProj, string xcodeTargetGuid, string xcodeTargetPath) { string projectPath = PBXProject.GetPBXProjectPath(xcodeTargetPath); if (xcodeModel.isOpenRestoreScene || xcodeModel.isHaveApple || xcodeModel.associatedDomains.Count > 0) { string entitlementsPath = xcodeModel.entitlementsPath; if (entitlementsPath == null || entitlementsPath == "" || !xcodeModel.entitlementsPath.Contains(".entitlements")) { string[] s = UnityEditor.PlayerSettings.applicationIdentifier.Split('.'); string productname = s[s.Length - 1]; entitlementsPath = "Unity-iPhone/" + productname + ".entitlements"; } #if UNITY_2018_1_OR_NEWER #if UNITY_2019_3_OR_NEWER ProjectCapabilityManager capManager = new ProjectCapabilityManager(projectPath, entitlementsPath, null, xcodeTargetGuid); #else ProjectCapabilityManager capManager = new ProjectCapabilityManager(projectPath, entitlementsPath, "Unity-iPhone"); #endif if (xcodeModel.associatedDomains.Count > 0 || xcodeModel.isHaveApple) { string[] domains = new string[xcodeModel.associatedDomains.Count]; int index = 0; foreach (string domainStr in xcodeModel.associatedDomains) { Debug.Log("AddCapabilityAssociatedDomains:" + domainStr); domains[index] = domainStr; index++; } //Debug.Log("xcodeTargetGuid:" + xcodeTargetGuid); //Debug.Log("xcodeTargetPath:" + xcodeTargetPath); //Debug.Log("projectPath:" + projectPath); //Debug.Log("GetUnityTargetName:" + PBXProject.GetUnityTargetName()); //Debug.Log("domainStr:" + MiniJSON.jsonEncode(domains)); if (capManager.GetType().GetMethod("AddAssociatedDomains") != null) { capManager.GetType().GetMethod("AddAssociatedDomains").Invoke(capManager, new object[] { domains }); } //Debug.Log("bundleIdentifier:" + UnityEditor.PlayerSettings.applicationIdentifier); //Debug.Log("productName:" + UnityEditor.PlayerSettings.productName); if (xcodeModel.isHaveApple && capManager.GetType().GetMethod("AddSignInWithApple") != null) { capManager.GetType().GetMethod("AddSignInWithApple").Invoke(capManager, null); } //推送 //capManager.AddPushNotifications(true); //内购 //capManager.AddInAppPurchase(); capManager.WriteToFile(); xcodeProj.AddCapability(xcodeTargetGuid, PBXCapabilityType.AssociatedDomains, xcodeTargetPath + "/" + entitlementsPath, true); } #else var entitlementFile = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE plist PUBLIC \" -//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"><plist version = \"1.0\"><dict>"; if (xcodeModel.associatedDomains.Count > 0 || xcodeModel.isHaveApple) { string[] domains = new string[xcodeModel.associatedDomains.Count]; int index = 0; foreach (string domainStr in xcodeModel.associatedDomains) { Debug.Log("AddCapabilityAssociatedDomains:" + domainStr); domains[index] = "<string>" + domainStr + "</string>"; index++; } var associatedDomainsString = ""; if (domains.Length > 0) { associatedDomainsString = "<key>com.apple.developer.associated-domains</key><array>" + string.Join("", domains) + "</array>"; } var appleString = ""; if (xcodeModel.isHaveApple) { appleString = "<key>com.apple.developer.applesignin</key><array><string> Default </string></array>"; } entitlementFile = entitlementFile + associatedDomainsString + appleString + "</dict></plist>"; } Debug.LogWarning(entitlementFile); Debug.LogWarning(xcodeTargetPath + "/" + entitlementsPath); Debug.LogWarning(xcodeTargetPath); StreamWriter sWriter = new StreamWriter(xcodeTargetPath + "/" + entitlementsPath); sWriter.WriteLine(entitlementFile); sWriter.Close(); sWriter.Dispose(); xcodeProj.AddBuildProperty(xcodeTargetGuid, "CODE_SIGN_ENTITLEMENTS", entitlementsPath); string fileGuid = (string)xcodeProj.AddFile(xcodeTargetPath + "/" + entitlementsPath, entitlementsPath, PBXSourceTree.Absolute); xcodeProj.AddFileToBuild(xcodeTargetGuid, fileGuid); #endif } }