跳轉至

Unity iOS

這指導了 Hive SDK Unity iOS 應用的構建和配置。

將權限添加到 Xcode 簽名和功能

將權限添加到您的 Xcode 項目中,以使用您從 Hive > ExternalDependency 選擇的功能。您可以使用 Hive PostProcess 編輯器或 HivePostProcess.cs 添加它們。

透過 Hive 後處理編輯器添加

從您的 Unity 開發環境運行 Hive PostProcess 編輯器,方法是轉到 Hive > 建置專案後處理設定 > iOSHive > 編輯配置 > 建置 iOS 專案後處理設定。然後,您可以選擇所需的 Hive SDK 功能並將權限添加到 Xcode 簽名與能力部分。

以下是可以添加的 Hive SDK 功能:

  • 推播通知:啟用本地和遠端推播功能。
    • 開發模式:在使用開發配置檔證書時設置。
    • 發行模式:在使用發行配置檔證書時設置。
  • 背景模式(遠端通知):決定應用程式在背景中時是否接收遠端推播通知。
  • Apple 登入:啟用使用 Apple 登入的功能。
  • 關聯域:允許用於通用連結的域名列表(允許從網站安裝和執行應用程式)。
  • 應用內購買:啟用應用內購買功能。
  • 遊戲中心:啟用遊戲中心登入及相關功能。


點擊 應用 以將選定的功能添加到 Xcode 簽名與功能。點擊 重置 以將設置重置為默認值。


配置的值以 JSON 字符串的形式保存在 Assets > Hive_SDK_v4 > PostProcess > iOS > HivePostProcessSettingIOS.txt 中。

Note

您也可以通过直接编辑HivePostProcessSettingiOS.txt文件的内容来修改Xcode项目的功能,而无需使用Hive PostProcess Editor。

透過 HivePostProcess.cs 添加

在 Unity 中构建 Xcode 项目之前,取消注释 HivePostProcess.cs 文件中的代码将自动将 Hive SDK 功能权限添加到 Xcode 签名和功能中。注释掉代码将删除权限。

在下面的代码中注释特定行将防止相应的权限被添加到签名和能力中。建议注释掉您不打算使用的权限的代码。

private static void iOSSettingCapabilites(string path, HivePostProcessSettingIOS setting) {
   #if UNITY_IOS
   Debug.Log("iOSSettingCapabilites : OnPostProcessBuild -\n path: "+path);

   var projPath = PBXProject.GetPBXProjectPath(path);
   var mainTargetName = "Unity-iPhone";
   var entitlementsFileName = "Entitlements.entitlements";

   ProjectCapabilityManager capabilityManager = new ProjectCapabilityManager(projPath, entitlementsFileName, mainTargetName);
    if (setting.isAddInAppPurchase) {
       capabilityManager.AddInAppPurchase();
   }
    if (setting.isAddPushNotifications) {
       // If you don't use push notification (Hive SDK notification) feature, comment out the line below.
       capabilityManager.AddPushNotifications(setting.isPushNotificationsDevelopment);
   }
    if (setting.isAddBackGroundModesRemoteNotifications) {
       capabilityManager.AddBackgroundModes(iOS.Xcode.BackgroundModesOptions.RemoteNotifications);
   }
    if (setting.isAddSignInWithApple) {
       // If you don't use Sign In With Apple feature, comment out the line below.
       capabilityManager.AddSignInWithApple();
   }
    if (setting.isAddAssociatedDomains) {
       var associatedDomains = setting.associatedDomains;
       capabilityManager.AddAssociatedDomains(associatedDomains.ToArray());
   }
    capabilityManager.WriteToFile();

   if (setting.isAddGameCenter) {
       var entitlementsModifier = new HiveEntitlementsModifier(path, entitlementsFileName);
       entitlementsModifier.AddGameCenter();
   }
   #endif
}
Warning

修改HivePostProcess.cs以自動添加或移除Hive SDK功能到info.plist文件和簽名與能力,僅在當前Hive SDK版本下可行。在修改HivePostProcess.cs的情況下,使用Hive SDK管理器更新Hive SDK版本可能會導致衝突。要無論Hive SDK版本如何都使用此功能,請避免修改HivePostProcess.cs,而是基於PBX Project API實現一個單獨的後處理腳本(例如GamePostProcess.cs)。

將詳細設置添加到 Xcode info.plist 文件

添加使用从 Hive > ExternalDependency 选择的功能所需的详细设置。在 Unity 中构建 Xcode 项目之前,取消注释 HivePostProcess.cs 文件中的代码将自动将使用 Hive SDK 的详细设置添加到 info.plist 文件中。注释掉代码将移除这些设置。

在下面的代码中注释掉特定行将防止相应的设置被添加到info.plist文件中。

private static void iOSSettingInfoPlist(string buildPath) {
   #if UNITY_IOS
    var PlistPath = buildPath + "/Info.plist";
   PlistDocument plist = new PlistDocument();
   plist.ReadFromFile(PlistPath);
    var rootDict = plist.root;
   rootDict.SetBoolean("UIViewControllerBasedStatusBarAppearance",false);
   if( hasFacebookAppId() )
       rootDict.SetString("FacebookAppID",getFacebookAppID());
   if (hasFacebookClientToken())
       rootDict.SetString("FacebookClientToken", getFacebookClientToken());
    rootDict.SetString("CFBundleIdentifier",getBundleIdentifier());
    //facebook white list
   var LSApplicationQueriesSchemes = rootDict.CreateArray("LSApplicationQueriesSchemes");
   LSApplicationQueriesSchemes.AddString("fbapi");
   LSApplicationQueriesSchemes.AddString("fb-messenger-share-api");
    // for vk
   LSApplicationQueriesSchemes.AddString("vk");
   LSApplicationQueriesSchemes.AddString("vkauthorize");
   LSApplicationQueriesSchemes.AddString("vk-share");
    // for WeChat
   LSApplicationQueriesSchemes.AddString("weixin");
   LSApplicationQueriesSchemes.AddString("weixinULAPI");

    // for QQ
   LSApplicationQueriesSchemes.AddString("mqqOpensdkSSoLogin");
   LSApplicationQueriesSchemes.AddString("mqqopensdkapiV2");
   LSApplicationQueriesSchemes.AddString("mqqopensdkapiV3");
   LSApplicationQueriesSchemes.AddString("wtloginmqq2");
   LSApplicationQueriesSchemes.AddString("mqq");
   LSApplicationQueriesSchemes.AddString("mqqapi");
   LSApplicationQueriesSchemes.AddString("mqqopensdknopasteboard");
   LSApplicationQueriesSchemes.AddString("mqqopensdknopasteboardios16");
   LSApplicationQueriesSchemes.AddString("lineauth2");
    //URL Types settings
   var CFBundleURLTypes = rootDict.CreateArray("CFBundleURLTypes");
    var facebookURLType = CFBundleURLTypes.AddDict();
   facebookURLType.SetString("CFBundleTypeRole","Editor");
   if( hasFacebookAppId() )
       facebookURLType.CreateArray("CFBundleURLSchemes").AddString("fb"+getFacebookAppID());
    // add Google reversed client id
   if (hasGoogleReversedClientId()){
       var googleReversedClientId = CFBundleURLTypes.AddDict();
       googleReversedClientId.SetString("CFBundleTypeRole","Editor");
       googleReversedClientId.CreateArray("CFBundleURLSchemes").AddString(getGoogleReversedClientId());
   }
   // add tencent appid
   if (hasQQAppId()){
       var qqAppId = CFBundleURLTypes.AddDict();
       qqAppId.SetString("CFBundleTypeRole","Editor");
       qqAppId.CreateArray("CFBundleURLSchemes").AddString("tencent"+getQQAppId());
   }
   // add VK appid
   if (hasVKAppId()) {
       var vkAppId = CFBundleURLTypes.AddDict();
       vkAppId.SetString("CFBundleTypeRole","Editor");
       vkAppId.CreateArray("CFBundleURLSchemes").AddString("vk"+getVKAppId());
   }
    // add wechat appid
   if (hasWeChatAppId()) {
       var weChatAppId = CFBundleURLTypes.AddDict();
       weChatAppId.SetString("CFBundleTypeRole","Editor");
       weChatAppId.CreateArray("CFBundleURLSchemes").AddString(getWeChatAppId());
   }
    // add line channel id
   if (hasLineChannelId()) {
       var lineChannelId = CFBundleURLTypes.AddDict();
       lineChannelId.SetString("CFBundleTypeRole","Editor");
       lineChannelId.CreateArray("CFBundleURLSchemes").AddString("line3rdp."+getBundleIdentifier());
   }
    var urlSchemes = CFBundleURLTypes.AddDict();
   urlSchemes.SetString("CFBundleTypeRole","Editor");
   urlSchemes.SetString("CFBundleIdentifier",getBundleIdentifier());
   urlSchemes.CreateArray("CFBundleURLSchemes").AddString(getBundleIdentifier());
    // remove exit on suspend if it exists.
   string exitsOnSuspendKey = "UIApplicationExitsOnSuspend";
   if(rootDict.values.ContainsKey(exitsOnSuspendKey))
   {
       rootDict.values.Remove(exitsOnSuspendKey);
   }
    // Set encryption usage boolean
   string encryptKey = "ITSAppUsesNonExemptEncryption";
   rootDict.SetBoolean(encryptKey, false);
    // ATS
   var ATSDict = rootDict.CreateDict("NSAppTransportSecurity");
   ATSDict.SetBoolean("NSAllowsArbitraryLoads",true);
    plist.WriteToFile(PlistPath);
    #endif
}
Warning

修改HivePostProcess.cs以自動添加或移除Hive SDK功能到info.plist文件和簽署與能力僅在當前Hive SDK版本下可行。在HivePostProcess.cs被修改的情況下使用Hive SDK管理器更新Hive SDK版本可能會導致衝突。要在不考慮Hive SDK版本的情況下使用此功能,請避免修改HivePostProcess.cs,而是基於PBX Project API實現一個單獨的後處理腳本(例如GamePostProcess.cs)。


HivePostProcess.cs 也可以用來鏈接特定文件。例如,當使用 Hive SDK 分析時,您可以在 Unity > Hive Dependencies 中啟用 Firebase。然後,將從 Google Firebase Console 下載的 GoogleService-Info.plist 文件放置在 Assets/ExternalResources/iOS 目錄中,並將以下代碼添加到 HivePostProcess.cs 中。這將自動將 GoogleService-Info.plist 文件鏈接到使用 Unity 構建的 Xcode 項目。

private static void iOSSettingProject(string buildPath) {
   ...
   // If you want to disable the feature of automatically adding GoogleService-Info.plist file to the Xcode project, comment out the line below.
   iOSAddGoogleServiceInfoPlist(buildPath, project, targets[mainTarget]);
   ...
}

#if UNITY_IOS
public static void iOSAddGoogleServiceInfoPlist(string buildPath, PBXProject project, string target) {

   var isUseFirebase = ExternalDependencyDictionaryManager.isUseDependency(ExternalDependencyType.Firebase);

   if (!isUseFirebase) {
       Debug.Log("[iOSAddGoogleServiceInfoPlist] ExternalDependencyType Firebase not use. Skip process.");
       return;
   }

   string[] filesToCopy = new string[]
   {
       "GoogleService-Info.plist",
   };

   foreach (string file in filesToCopy) {
       var srcPath = Path.Combine("Assets/ExternalResources/iOS", file);
       var relativePath = Path.Combine("", file);
       var physicalPath = Path.Combine(buildPath, relativePath);

       if (!File.Exists(srcPath)) {
           Debug.Log("[iOSAddGoogleServiceInfoPlist] " + file + " not exist. Skip process.");
           continue;
       }

       Debug.Log("[iOSAddGoogleServiceInfoPlist] srcPath = " + srcPath);
       Debug.Log("[iOSAddGoogleServiceInfoPlist] relativePath = " + relativePath);
       Debug.Log("[iOSAddGoogleServiceInfoPlist] physicalPath = " + physicalPath);

       File.Copy(srcPath, physicalPath, true);
       var fileGuid = project.AddFile(physicalPath, relativePath);
       Debug.Log("[iOSAddGoogleServiceInfoPlist] fileGuid = " + fileGuid);

       project.AddFileToBuild(target, fileGuid);
   }
} 
#endif

刪除未使用的 XML 檔案

如果您是通过 Unity 包 安装或更新 Hive SDK Unity,而不是通过 Hive SDK 管理器,您必须在构建应用程序之前删除以下路径中所有 HIVESDK_{library_name}Dependencies.xml 文件。

  • 資產/Hive_SDK_v4/依賴
  • 資產/Hive_SDK_v4/編輯器

要刪除的文件示例如下:

  • HIVESDK_DefaultDependencies.xml
  • HIVESDK_HerculesDependencies.xml
  • HIVESDK_HiveAnalyticsConsentModeDependencies.xml
  • HIVESDK_HiveDataStoreDependencies.xml
  • HIVESDK_HiveDeviceManagementDependencies.xml
  • HIVESDK_HiveGoogleInAppReviewDependencies.xml
  • HIVESDK_HiveGoogleInAppUpdateDependencies.xml
  • HIVESDK_HivePromotionDependencies.xml
  • HIVESDK_HiveRealNameVerificationDependencies.xml
  • HIVESDK_MarketAmazonDependencies.xml
  • HIVESDK_MarketAppleDependencies.xml
  • HIVESDK_MarketGoogleDependencies.xml
  • HIVESDK_MarketHiveStoreDependencies.xml
  • HIVESDK_MarketHuaweiDependencies.xml
  • HIVESDK_MarketLebiDependencies.xml
  • HIVESDK_MarketOneStoreDependencies.xml
  • HIVESDK_MarketSamsungDependencies.xml
  • HIVESDK_ProviderAdjustDependencies.xml
  • HIVESDK_ProviderAppleDependencies.xml
  • HIVESDK_ProviderAppleSignInDependencies.xml
  • HIVESDK_ProviderAppsFlyerDependencies.xml
  • HIVESDK_ProviderFacebookDependencies.xml
  • HIVESDK_ProviderFirebaseDependencies.xml
  • HIVESDK_ProviderGoogleDependencies.xml
  • HIVESDK_ProviderGooglePlayGamesDependencies.xml
  • HIVESDK_ProviderHiveMembershipDependencies.xml
  • HIVESDK_ProviderHuaweiDependencies.xml
  • HIVESDK_ProviderLineDependencies.xml
  • HIVESDK_ProviderQQDependencies.xml
  • HIVESDK_ProviderRecaptchaDependencies.xml
  • HIVESDK_ProviderSingularDependencies.xml
  • HIVESDK_ProviderVKDependencies.xml
  • HIVESDK_ProviderWechatDependencies.xml
  • HIVESDK_ProviderWeverseDependencies.xml
  • HIVESDK_PushAmazonAdmDependencies.xml
  • HIVESDK_PushGoogleFcmDependencies.xml