xcode 通过代码打包设置

xcode打包部分设置的脚本如下

public class XcodeSetting : MonoBehaviour
{
    private static List<Menu> menuList;

    [PostProcessBuild(999)]
    public static void OnPostprocessBuild(BuildTarget BuildTarget, string path)
    {
        if (BuildTarget == BuildTarget.iOS)
        {
            Debug.Log("OnPostprocessBuild ProjectPath:" + path);
            string projPath = PBXProject.GetPBXProjectPath(path);//获取.xcodeproj文件的路径

            PBXProject proj = new PBXProject();//new()一个PBXProject对象,然后从上面获取的路径中读出字符串。
            string contents = File.ReadAllText(projPath);
            proj.ReadFromString(contents);

            string target = proj.TargetGuidByName(PBXProject.GetUnityTargetName());//获取targetGUID

            // 链接器
            proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO");//bitcode是被编译程序的一种中间形式的代码。包含bitcode配置的程序将会在App store上被编译和链接。bitcode允许苹果在后期重新优化我们程序的二进制文件(我们第三方库不一定支持,所以要设置为NO)
            proj.SetBuildProperty (target, "OTHER_LDFLAGS", "-Objc -all_load -lstdc++.6.0.9 -lsqlite3");//Other Linker Flags  在ios开发过程中,有时候会用到第三方的静态库(.a文件),然后导入后发现编译正常但运行时会出现selector not recognized的错误,从而导致app闪退。

            //pbxProj.AddBuildProperty(targetGuid, "FRAMEWORK_SEARCH_PATHS", "$(SRCROOT)/Libraries/BabyFrameWork/**");
            //pbxProj.AddBuildProperty(targetGuid, "HEADER_SEARCH_PATHS", "$(SRCROOT)/Libraries/BabyFrameWork/**");
            //pbxProj.AddBuildProperty(targetGuid, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)/Libraries/BabyFrameWork/**");
            //------------------------拷贝系统Framework----------------------------------------------
            string xcodePath = Application.dataPath + "/xcode.txt";
            xcodePath = xcodePath.Replace("Assets/", "");
            FileStream xcode_fs = new FileStream(xcodePath, FileMode.Open, FileAccess.Read);
            StreamReader xcode_sr = new StreamReader(xcode_fs);//仅 对文本 执行  读写操作 
            string line = null;
            while((line= xcode_sr.ReadLine())!= null)
            {
                Debug.Log ("framework=" + line);
                string frameWorkName = line.Split('.')[0];
                string filterName = line.Split ('.') [1];
                if (filterName == "framework") {
                    if(frameWorkName == "JavaScriptCore")
                    {
                        proj.AddFrameworkToProject(target, line, true);//这里第一个参数是第三部中获取到的GUID,第二个参数是framework名(这里一定要是.framework为后缀),第三个参数是用来设置framework是require还是optional。
                    }
                    else
                    {
                        proj.AddFrameworkToProject(target, line, false);//这里第一个参数是第三部中获取到的GUID,第二个参数是framework名(这里一定要是.framework为后缀),第三个参数是用来设置framework是require还是optional。
                    }
                }
                else
                {
                    proj.AddFileToBuild (target, proj.AddFile("usr/lib/"+line, "Frameworks/"+line, PBXSourceTree.Sdk));
                }
            }
            //C#读取TXT文件之关上文件,留心顺序,先对文件内部执行 关上,然后才是文件~     
            xcode_sr.Close();
            xcode_fs.Close();
            //--------------------------拷贝系统Framework end-------------------------------------


            File.WriteAllText(projPath, proj.WriteToString());


            //------------------------------APPID-----------------------------------------------------
            string txtPath = Application.dataPath + "/Resources/AppId.txt";
            FileStream fs = new FileStream(txtPath, FileMode.Open, FileAccess.Read); 
            StreamReader sr = new StreamReader(fs);//仅 对文本 执行  读写操作 
            int AppId = int.Parse(sr.ReadToEnd());
            //C#读取TXT文件之关上文件,留心顺序,先对文件内部执行 关上,然后才是文件~     
            sr.Close();
            fs.Close();

            menuList = ExcelAccess.SelectMenuTable(1, AppId);
            if (menuList != null)
            {
                if (menuList.Count > 0)
                {
                    Menu menu = menuList[0];

                    string wxAppid = menu.WXAppID;
                    PlistElementDict dict;

                    //UrlType
                    //Handle plist  
                    string plistPath = path + "/Info.plist";
                    PlistDocument plist = new PlistDocument();
                    plist.ReadFromString(File.ReadAllText(plistPath));
                    PlistElementDict rootDict = plist.root;

                    //NSContactsUsageDescription->通讯录
                    //NSMicrophoneUsageDescription->麦克风
                    //NSPhotoLibraryUsageDescription->相册
                    //NSCameraUsageDescription->相机
                    //NSLocationAlwaysUsageDescription->地理位置
                    //NSLocationWhenInUseUsageDescription->地理位置
                    rootDict.SetString("NSLocationAlwaysUsageDescription", "地理位置相近的玩家不可进入同一个牌桌");
                    rootDict.SetString("NSLocationUsageDescription", "地理位置相近的玩家不可进入同一个牌桌");
                    rootDict.SetString("NSLocationWhenInUseUsageDescription", "地理位置相近的玩家不可进入同一个牌桌");
                    rootDict.SetString("NSMicrophoneUsageDescription", "使用麦克风");
                    rootDict.SetString("NSPhotoLibraryUsageDescription", "使用相册");
                    rootDict.SetString("NSPhotoLibraryAdditionsUsageDescription","需要访问您的相册");

                    //PList文件添加微信为白名单
                    PlistElementArray array = rootDict.CreateArray("LSApplicationQueriesSchemes");
                    array.AddString("weixin");

                    // 设置支持HTTP
                    dict = rootDict.CreateDict("NSAppTransportSecurity");
                    dict.SetBoolean("NSAllowsArbitraryLoads", true);

                    PlistElementArray urlTypes = rootDict.CreateArray("CFBundleURLTypes");
                    // add weixin url scheme应用需要在“Info.plist”中将要使用的URL Schemes列为白名单,才可正常检查其他应用是否安装。
                    PlistElementDict wxUrl = urlTypes.AddDict();
                    wxUrl.SetString("CFBundleTypeRole", "Editor");
                    wxUrl.SetString("CFBundleURLName", "weixin");
                    PlistElementArray wxUrlScheme = wxUrl.CreateArray("CFBundleURLSchemes");
                    wxUrlScheme.AddString(wxAppid);

                    
                    //add csmj url scheme
                    PlistElementDict appUrl = urlTypes.AddDict();
                    appUrl.SetString("CFBundleTypeRole", "Editor");
                    appUrl.SetString("CFBundleURLName", "chaoshanmajiang");
                    PlistElementArray appUrlScheme = appUrl.CreateArray("CFBundleURLSchemes");
                    appUrlScheme.AddString("elephant"+ menu.ID);


                    //add location appkey url scheme
                    PlistElementDict locationUrl = urlTypes.AddDict();
                    locationUrl.SetString("CFBundleTypeRole", "Editor");
                    locationUrl.SetString("CFBundleURLName", "locationAppKey");
                    PlistElementArray locationScheme = locationUrl.CreateArray("CFBundleURLSchemes");
                    locationScheme.AddString("xx"+menu.Location_AppKey);

                    File.WriteAllText(plistPath, plist.WriteToString());
                }
            }

            CopyAndReplaceIcon (path);
        }
    }

    static void CopyAndReplaceIcon (string projectPath)
    {
        string targetWXShareIconPath = projectPath + "/SDK/WX/res2.png";
        string sourceIconPath = System.Environment.CurrentDirectory + "/Assets/Art/ICON/IOS/57.png";
        Debug.Log (string.Format ("CopyAndReplaceIcon from {0} to {1}", sourceIconPath, targetWXShareIconPath));
        File.Copy (sourceIconPath, targetWXShareIconPath, true);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值