Unity iOS
本指南介绍了 Hive SDK Unity iOS 应用程序的构建和配置。
在 Xcode 签名与能力中添加权限¶
为您的 Xcode 项目添加权限,以使用您从 Hive > ExternalDependency 选择的功能。您可以使用 Hive PostProcess 编辑器或 HivePostProcess.cs 添加它们。
通过 Hive 后处理编辑器添加¶
在您的 Unity 开发环境中,通过转到 Hive > Build project post process settings > iOS 或 Hive > Edit Config > Build iOS project post process settings 来运行 Hive PostProcess 编辑器。然后,您可以选择所需的 Hive SDK 功能,并将权限添加到 Xcode 签名和能力部分。
以下是可以添加的 Hive SDK 功能:
- 推送通知:激活本地和远程推送功能。
- 开发模式:在使用开发配置文件证书时设置。
- 发布模式:在使用发布配置文件证书时设置。
- 后台模式(远程通知):确定应用在后台时是否接收远程推送通知。
- Apple 登录:激活使用 Apple 登录的功能。
- 关联域:允许用于通用链接的域列表(允许从网站安装和执行应用)。
- 应用内购买:激活应用内购买功能。
- 游戏中心:激活游戏中心登录及相关功能。
点击 应用 以将选定的功能添加到 Xcode 签名与能力。点击 重置 以将设置重置为默认值。
配置的值作为 JSON 字符串保存在 Assets > Hive_SDK_v4 > PostProcess > iOS > HivePostProcessSettingIOS.txt 中。
Note
您还可以通过直接编辑HivePostProcessSettingiOS.txt文件的内容来修改Xcode项目的功能,而无需使用Hive后处理编辑器。
通过 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文件和Signing & Capabilities,仅在当前Hive SDK版本下可行。在HivePostProcess.cs被修改的情况下,通过Hive SDK Manager更新Hive SDK版本可能会导致冲突。为了在不考虑Hive SDK版本的情况下使用此功能,请避免修改HivePostProcess.cs,而是实现一个独立的后处理脚本(例如,GamePostProcess.cs),基于PBX Project API。
HivePostProcess.cs 也可以用来链接特定文件。例如,当使用 Hive SDK 分析时,您可以在 Unity > Hive Dependencies 中启用 Firebase。然后,在将从 Google Firebase 控制台 下载的 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