IOS 平台相关内容

IOS Build Config

FrameworkId

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/// <summary>  
/// 添加系统库
/// </summary>
/// <param name="pbxProject"></param>
/// <param name="mFrameworkGuid"></param>
private static void SystemFrameworkAdd(PBXProject pbxProject, string mFrameworkGuid)
{
string[] frameworksToAdd =
{ //压缩库
"libz.dylib",
"libsqlite3.tbd", //IAD是苹果推出的广告平台
"iAd.framework",
//获取设备唯一的一个广告标识符
"AdSupport.framework",
//输出手机的运营商信息
"CoreTelephony.framework",
//数字商品或服务
"StoreKit.framework",
//认证服务
"AuthenticationServices.framework",
//请求用户授权,获取对可用于跟踪用户或设备的应用程序相关数据的访问权限
"AppTrackingTransparency.framework",
//账户管理框架
"Accounts.framework",
//社交框架为中国用户提供统一的API,用于与社交网络(包括Twitter 和Facebook)以及新浪微博交互
"Social.framework",
//进行大规模的数学计算和图像计算,优化为高性能和低能耗
"Accelerate.framework",
//框架用于保证应用程序所管理之数据的安全
"Security.framework",
//一套基于 C 的API 框架,使用了Quartz 作为绘图引擎,可用于一切绘图操作
"CoreGraphics.framework",
//读取异常发生的系统信息
"SystemConfiguration.framework",
//WX
"WebKit.framework",
};
foreach (var key in frameworksToAdd)
{
//如果框架是可选框架(即弱关联框架),则此参数为 true,如果框架是必需框架,则为 false。
pbxProject.AddFrameworkToProject(mFrameworkGuid, key, true);
}}

系统设定

1
2
3
4
5
6
7
8
9
10
11
12
//使用存储结构: com.yuqu.xfish. //添加keychain 
ProjectCapabilityManager projectCapabilityManager = new ProjectCapabilityManager(projectPath, "U3DFishing.entitlements", "Unity-iPhone");
projectCapabilityManager.AddSignInWithApple();
projectCapabilityManager.AddKeychainSharing(new[]
{
"$(AppIdentifierPrefix)" + Application.identifier});
projectCapabilityManager.AddPushNotifications(false);
projectCapabilityManager.AddAssociatedDomains(new[]
{
"applinks:wx.ifishgames.cn"});
projectCapabilityManager.AddBackgroundModes(BackgroundModesOptions.None);
projectCapabilityManager.WriteToFile();

Plist 修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/// <summary>  
/// 修改pList
/// </summary>
private static void PlistModify(string plistPath)
{
var plist = new PlistDocument();
plist.ReadFromFile(plistPath);
PlistElementArray urlArray = null;
//Add BackgroundModes
if (!plist.root.values.ContainsKey("UIBackgroundModes"))
urlArray = plist.root.CreateArray("UIBackgroundModes");
else
urlArray = plist.root.values["UIBackgroundModes"].AsArray();
urlArray.values.Clear();
//选择语言(字符串)
plist.root.SetString("CFBundleDevelopmentRegion", "en");
plist.root.SetString("NSUserTrackingUsageDescription", "是否允许我们的APP使用您的IDFA信息,以此来优化您的使用体验,定向提供服务");
plist.root.SetBoolean("manageAppVersionAndBuildNumber", false); //苹果默认会把其他library改掉,加这个不让它改
plist.root.SetBoolean("ITSAppUsesNonExemptEncryption", false); //苹果出口合格证明信息
//fb
plist.root.SetString("FacebookAppID", "730836728394597");
plist.root.SetString("FacebookClientToken", "8ee410aa04968c16d0f19df37542dbd6");
plist.root.SetString("FacebookDisplayName", "捕魚新紀元");
plist.root.SetBoolean("FacebookAutoLogAppEventsEnabled", true); //FB自动记录事件功能
plist.root.SetBoolean("FacebookAdvertiserIDCollectionEnabled", true); //FB广告主编号收集功能
//设置LSApplicationQueriesSchemes(数组)
PlistElementArray loginChannelsArr = plist.root.CreateArray("LSApplicationQueriesSchemes");
string[] applicationQueriesSchemes = new[]
{
//wx
"weixin", "weixinULAPI","weixinURLParamsAPI",
//qq
"time","mqq","mqqapi","mqqbrowser","mttbrowser","mqqOpensdkSSoLogin","mqqopensdkapiV2","mqqopensdkapiV4",
"mqzone","mqzoneopensdk","mqzoneopensdkapi","mqzoneopensdkapi19","mqzoneopensdkapiV2","mqqapiwallet",
"mqqopensdkfriend","mqqopensdkavatar","mqqopensdkminiapp","mqqopensdkdataline","mqqgamebindinggroup",
"mqqopensdkgrouptribeshare","tencentapi.qq.reqContent","tencentapi.qzone.reqContent","mqqthirdappgroup",
"mqqopensdklaunchminiapp","mqqopensdkproxylogin","mqqopensdknopasteboard","mqqopensdknopasteboardios16",
"mqqopensdkcheckauth","mqqguild",
//fb
"fbapi","fbapi20130214","fbapi20130410","fbapi20130702","fbapi20131010","fbapi20131219",
"fbapi20140410","fbapi20140116","fbapi20150313","fbapi20150629","fbapi20160328",
"fbauth","fbauth2","fb-messenger-share-api"
};
foreach (string scheme in applicationQueriesSchemes)
{
loginChannelsArr.AddString(scheme);
}
PlistElementArray urlTypes = plist.root.CreateArray("CFBundleURLTypes"); //wx
PlistElementDict itemDict = urlTypes.AddDict();
itemDict.SetString("CFBundleTypeRole", "Editor");
itemDict.SetString("CFBundleURLName", "wexin");
PlistElementArray wxSchemesArray = itemDict.CreateArray("CFBundleURLSchemes");
wxSchemesArray.AddString("wxea4955cb701dc6f4"); //qq
itemDict = urlTypes.AddDict();
itemDict.SetString("CFBundleTypeRole", "Editor");
itemDict.SetString("CFBundleURLName", "tencentopenapi");
PlistElementArray qqSchemesArray = itemDict.CreateArray("CFBundleURLSchemes");
qqSchemesArray.AddString("tencent101963101");//fb
itemDict = urlTypes.AddDict();
itemDict.SetString("CFBundleTypeRole", "Editor");
itemDict.SetString("CFBundleURLName", "facebookapi");
PlistElementArray fbSchemesArray = itemDict.CreateArray("CFBundleURLSchemes");
fbSchemesArray.AddString("fb730836728394597");
File.WriteAllText(plistPath, plist.WriteToString());
}

Unity 插入代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var codeBasePath = Path.GetFullPath(path);  
UnityAppControllerCodesAdd(codeBasePath);
private static void UnityAppControllerCodesAdd(string codeBasePath)
{
var orientationSupport = Path.GetFullPath(codeBasePath + "/MainApp/main.mm");
if (File.Exists(orientationSupport))
{
var allTxt = File.ReadAllLines(orientationSupport);
allTxt[0] = @"#include ""../UnityFramework/UnityFramework.h""";
File.WriteAllLines(orientationSupport, allTxt);
}
else
{
Log.Warn($"[打包App]-->IOS: OnPostprocessBuild file {orientationSupport} not found!");
}
}

OnPostprocessBuild

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void OnPostprocessBuild(BuildTarget buildTarget, string path)  
{
if (buildTarget != BuildTarget.iOS){ return;}
Log.Info($"[打包] Target: {buildTarget}---> Path: {path}");
var projPath = Path.Combine(path, "Unity-iPhone.xcodeproj/project.pbxproj"); var pbxProject = new PBXProject(); pbxProject.ReadFromString(File.ReadAllText(projPath)); var targetGuid = pbxProject.GetUnityFrameworkTargetGuid(); var xcodeTarget = pbxProject.GetUnityMainTargetGuid(); var projectGuid = pbxProject.ProjectGuid(); //添加系统库
SystemFrameworkAdd(pbxProject, targetGuid);
//在Other Linker Flags 添加 -ObjC //ios 12 闪退 -ld_classic pbxProject.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-ld_classic");
//设置UnityFramework
pbxProject.SetBuildProperty(targetGuid, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "NO");
pbxProject.SetBuildProperty(xcodeTarget, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "YES"); pbxProject.SetBuildProperty(projectGuid, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "NO");
//pbxProject.AddFileToBuild(frameworkId, pbxProject.FindFileGuidByProjectPath("Data"));
pbxProject.WriteToFile(projPath);

//插入代码
var codeBasePath = Path.GetFullPath(path);
UnityAppControllerCodesAdd(codeBasePath);
ProjectCapabilityModify(projPath); //2、修改Info.plist文件
var plistPath = Path.Combine(path, "Info.plist");
PlistModify(plistPath); // 添加文件
FilesAdd(pbxProject, xcodeTarget, path, projPath);
}