简单来说就是unity提供一套api去修改xcode项目工程配置以及修改plist文件内容(当unity build结束后, 会自动回调OnPostProcessBuild).

以下是一些用到的配置处理:

ENABLE_BITCODE
AddFramework
https
NSPhotoLibraryUsageDescription
 [PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget BuildTarget, string path)
{
if (BuildTarget == BuildTarget.iOS)
{
UnityEngine.Debug.Log("XCodePostProcess: Starting to perform post build tasks for iOS platform."); /*======== projPath ========*/
string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj"; PBXProject proj = new PBXProject();
proj.ReadFromFile(projPath); string target = proj.TargetGuidByName("Unity-iPhone"); // ENABLE_BITCODE=False
proj.SetBuildProperty(target, "ENABLE_BITCODE", "false"); // add extra framework(s)
proj.AddFrameworkToProject(target, "Security.framework", false);
proj.AddFrameworkToProject(target, "CoreTelephony.framework", true);
proj.AddFrameworkToProject(target, "libz.tbd", true); // rewrite to file
File.WriteAllText(projPath, proj.WriteToString()); string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath)); // Get root
PlistElementDict rootDict = plist.root; /* ipad 关闭分屏 */
rootDict.SetBoolean("UIRequiresFullScreen", true); var now = System.DateTime.Now;
string time = string.Format("{0}_{1}_{2} {3}:{4}:{5}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
/* 设置Build值 */
rootDict.SetString("CFBundleVersion", string.Format("{0}({1})", GlobalVars.VERSION, time)); /* iOS9所有的app对外http协议默认要求改成https */
// Add value of NSAppTransportSecurity in Xcode plist
var atsKey = "NSAppTransportSecurity"; PlistElementDict dictTmp = rootDict.CreateDict( atsKey );
dictTmp.SetBoolean( "NSAllowsArbitraryLoads", true); // location native development region
rootDict.SetString("CFBundleDevelopmentRegion", "zh_CN"); // for share sdk 截屏
rootDict.SetString("NSPhotoLibraryUsageDescription", "We need use photo library usage"); // Write to file
File.WriteAllText(plistPath, plist.WriteToString()); }
}

在Info.plist里添加URL Scheme:

 /*======== plist ========*/
string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath)); // Get root
PlistElementDict rootDict = plist.root; // URL schemes 追加
var urlTypeArray = plist.root.CreateArray("CFBundleURLTypes");
var urlTypeDict = urlTypeArray.AddDict();
urlTypeDict.SetString("CFBundleTypeRole", "Editor");
urlTypeDict.SetString("CFBundleURLName", "OpenApp");
var urlScheme = urlTypeDict.CreateArray("CFBundleURLSchemes");
urlScheme.AddString("sampleApp"); urlTypeDict = urlTypeArray.AddDict();
urlTypeDict.SetString("CFBundleURLName", "com.abc.sampleApp");
urlScheme = urlTypeDict.CreateArray("CFBundleURLSchemes");
urlScheme.AddString("sampleApp2");

有些第三方的sdk需要在appController里加一些“料”,比如Adjust SDK的deeplink功能,需要在openUrl里加[Adjust appWillOpenUrl:url];,也需要在continueUserActivity函数里作相应处理。这里的处理参考了雨松大大的博客。

首先是XClass这个类:

 using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO; namespace UnityEditor.XCodeEditor
{
public partial class XClass : System.IDisposable
{ private string filePath; public XClass(string fPath)
{
filePath = fPath;
if (!System.IO.File.Exists(filePath))
{
Debug.LogError(filePath + "not found in path.");
return;
}
} public void WriteBelow(string below, string text)
{
StreamReader streamReader = new StreamReader(filePath);
string text_all = streamReader.ReadToEnd();
streamReader.Close(); int beginIndex = text_all.IndexOf(below);
if (beginIndex == -)
{
Debug.LogError(filePath + " not found sign in " + below);
return;
} int endIndex = text_all.LastIndexOf("\n", beginIndex + below.Length); text_all = text_all.Substring(, endIndex) + "\n" + text + "\n" + text_all.Substring(endIndex); StreamWriter streamWriter = new StreamWriter(filePath);
streamWriter.Write(text_all);
streamWriter.Close();
} public void Replace(string below, string newText)
{
StreamReader streamReader = new StreamReader(filePath);
string text_all = streamReader.ReadToEnd();
streamReader.Close(); int beginIndex = text_all.IndexOf(below);
if (beginIndex == -)
{
Debug.LogError(filePath + " not found sign in " + below);
return;
} text_all = text_all.Replace(below, newText);
StreamWriter streamWriter = new StreamWriter(filePath);
streamWriter.Write(text_all);
streamWriter.Close(); } public void Dispose()
{ }
}
}

然后还是在OnPostprocessBuild里进行hack:

 string xcodePath = Path.GetFullPath (path);
UnityEditor.XCodeEditor.XClass UnityAppController = new UnityEditor.XCodeEditor.XClass(xcodePath + "/Classes/UnityAppController.mm");
UnityAppController.WriteBelow("#include \"PluginBase/AppDelegateListener.h\"", "#include \"Adjust.h\"");
UnityAppController.WriteBelow("AppController_SendNotificationWithArg(kUnityOnOpenURL, notifData);","[Adjust appWillOpenUrl:url];");
UnityAppController.WriteBelow("SensorsCleanup();\n}", "- (BOOL)application:(UIApplication*)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * _Nullable))restorationHandler\r{\rif ([[userActivity activityType] isEqualToString:NSUserActivityTypeBrowsingWeb])\r{\rNSURL *url = [userActivity webpageURL];\rNSURL *oldStyleDeepLink = [Adjust convertUniversalLink:url scheme:@\"sampleApp\"];\r[Adjust appWillOpenUrl:url];\r}\rreturn YES;\r}");

还在研究怎么加XCode Entitlement文件中。。。

使用PostProcessBuild设定Unity产生的Xcode Project的更多相关文章

  1. Differences Between Xcode Project Templates for iOS Apps

    Differences Between Xcode Project Templates for iOS Apps When you create a new iOS app project in Xc ...

  2. xcode project

    An Xcode project is a repository for all the files, resources, and information required to build one ...

  3. vapor 生成xcode project 产生的错误解决方式

    运行vapor xcode时报错: Could not generate Xcode project: error: terminated(72): xcrun --sdk macosx --find ...

  4. Could not automatically select an Xcode project

    当把CocoaPods生成的workspace移动到上层目录时,需要改下Pods.xcconfig和工程里的一些设置,就通常没什么难度. 当遇到这个问题时: Could not automatical ...

  5. App Distribution Guide--(三)---Configuring Your Xcode Project for Distribution

    Configuring Your Xcode Project for Distribution You can edit your project settings anytime, but some ...

  6. Unity导出的Xcode工程目录

    Classes文件夹: Unity Runtime和ObjectC代码 main.mm和AppController.mm:应用程序入口点 iPhone_Profiler.h:定义了启用内部分析器(In ...

  7. Unity导出的Xcode项目,iOS端管理摄像头的方法

    Vuforia导出的工程中管理摄像头问题 在以前的篇幅中提到了unity端和iOS端的动态交互.现在出现了一个问题.因为设备上的摄像机是实例化过来的.并且是一个单例.unity虽然已经不再显示了.但是 ...

  8. Xcode project 设置相关

    FauxPas 这是一款Mac平台的用于检查Xcode项目的辅助工具 ,可以帮助我们找出常见的错误.隐藏的bug.不良实践以及可维护性问题和风格问题. 一, $(SRCROOT)  :当前工程所在的目 ...

  9. iOS将Unity导出的Xcode工程导入到另一个Xcode项目, 及常见报错的解决方法

    demo下载地址 http://pan.baidu.com/s/1pLcpKpl 1.Unity导出工程时设置bundle id要与项目一致 2.修改bit code为NO 3.删除Main.stor ...

随机推荐

  1. 《连载 | 物联网框架ServerSuperIO教程》- 18.集成OPC Client,及使用步骤

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...

  2. C++命名空间的解释 【转】

    使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突.在C++中,变量.函数和类都是大量存在的.如果没有命名空间,这些变量.函数.类的名称将都存在于全局命名空间中,会导致很多冲突.比如,如果我 ...

  3. SEO,搜索引擎优化原理方法等整体把握

    SEO 搜索算法: 全文文字 title 标签,title里面的文字 link 链接 link 链接里的文字 站点信任度 最佳实践: 一.设置title 准确的描述当前网页的内容 提高站点内title ...

  4. Ubuntu中文本地化后字体改变

    ubuntu中文本地化后会安装2个字体 fonts-arphic-ukai fonts-arphic-uming 找到这两个字体删除之.. sudo apt- get remove fonts-arp ...

  5. ST-LINK调试完成

    今天真是一波三折啊. 买回来的st-link刚开始不会用,各种百度,还好有两个很好的教程.连接发在下面吧. http://blog.csdn.net/TXF1984/article/details/4 ...

  6. 读APUE分析散列表的使用

    最近学习APUE读到避免线程死锁的部分,看到部分源码涉及到避免死锁部分,源码使用了散列表来实现对结构(struct)的存储与查找. 本文不讨论代码中的互斥量部分. #include <stdli ...

  7. JQuery hover鼠标变换

    一般而言,我们为非按钮.链接等元素添加hover事件时,虽然能够处理悬停事件,但是鼠标却并没有变化,会造成悬停事件不明显的结果,为此,我们可以添加CSS样式cursor:pointer,使得该元素的悬 ...

  8. 搭建本地git仓库

    使用工具:git|码云 步骤: 注册码云账号,创建项目名称等. 本地git配置 本地文件目录:git init(初始化创建分支master) 基础配置:git config --global user ...

  9. 【css笔记】css中的盒模型和三种定位机制(固定定位,绝对定位,浮动)

    html页面上的元素都可以看成是框组成的,框通过三种定位机制排列在一起就过程了我们看到的页面.而框就是盒模型. 盒模型 1.页面上的每个元素可以看成一个矩形框,每个框由元素的内容,内边距,边框和外边距 ...

  10. OC点语法介绍和使用以及@property关键字

    使用"点语法" Person *p =[Person new]; //点语法 //对象.属性名 //注意,此时 (p.age)并不是直接方法实例对象 //而是xcode可能到点语法 ...