Unity 打包到XCode自动化设置参数

    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget buildTarget, string buildPath)
    {
        if (buildTarget != BuildTarget.iOS)
            return;

        string pbxProjPath = PBXProject.GetPBXProjectPath(buildPath);
        var pbxProject = new PBXProject();
        pbxProject.ReadFromString(File.ReadAllText(pbxProjPath));
        string targetGuid = pbxProject.GetUnityMainTargetGuid();
        string frameworkGuid = pbxProject.GetUnityFrameworkTargetGuid();


        // 设置关闭Bitcode
        pbxProject.SetBuildProperty(targetGuid, ENABLE_BITCODE_KEY, "NO");
        pbxProject.SetBuildProperty(frameworkGuid, ENABLE_BITCODE_KEY, "NO");

        // xcode archive filed
        var token = pbxProject.GetBuildPropertyForAnyConfig(targetGuid, "USYM_UPLOAD_AUTH_TOKEN");
        if (string.IsNullOrEmpty(token))
        {
            token = "FakeToken";
        }
        pbxProject.SetBuildProperty(targetGuid, USYM_UPLOAD_AUTH_TOKEN, token);
        pbxProject.SetBuildProperty(frameworkGuid, USYM_UPLOAD_AUTH_TOKEN, token);

        //添加系统库(Unity默认会添加部分库,不能添加重复的库,否则会有问题)
        //例 
        pbxProject.AddFrameworkToProject(frameworkGuid, "CoreTelephony.framework", false);


        //添加tbd
        //例 
        string fileGuidSqlite = pbxProject.AddFile("usr/lib/libsqlite3.tbd", "Libraries/libsqlite3.tbd", PBXSourceTree.Sdk);

        //添加引用的资源 如 .bundle文件
        //例 
        string p = .bundle地址;

        string bun = pbxProject.AddFile(p, "名称.bundle", PBXSourceTree.Absolute);
        pbxProject.AddFileToBuild(targetGuid,  bun);

        //修改Info.plist文件
        SetInfoPlist(buildPath);

        File.WriteAllText(pbxProjPath, pbxProject.WriteToString());

        UnityEngine.Debug.Log("PBXProject : ---->" + pbxProject.WriteToString());
    }

    public static void SetInfoPlist(string buildPath)
    {
        List<string> privacySensiticeData = new List<string>();

        PlistDocument plist = GetInfoPlist(buildPath);

        //选择语言(字符串)
        plist.root.SetString("NSPhotoLibraryAddUsageDescription", "此App需要您的同意,才能保存图片到您的相册");
        plist.root.SetString("NSPhotoLibraryUsageDescription", "此App需要您的同意才能读取媒体资料库");
        //(bool值)
        plist.root.SetBoolean("ITSAppUsesNonExemptEncryption", false);

        //设置LSApplicationQueriesSchemes(数组)
        //例
        PlistElementArray loginChannelsArr;
        loginChannelsArr = plist.root.CreateArray("LSApplicationQueriesSchemes");
        loginChannelsArr.AddString("mqqapi");
        loginChannelsArr.AddString("mqq");

        //配置(字典)
        PlistElementDict plistDic;
        plistDic = plist.root.CreateDict("NSAppTransportSecurity");
        plistDic.SetBoolean("NSAllowsArbitraryLoads", true);

        // 添加 url scheme 
        PlistElementArray urlTypes = plist.root.CreateArray("CFBundleURLTypes");
        PlistElementDict wxUrl = urlTypes.AddDict();
        wxUrl.SetString("CFBundleTypeRole", "Editor");
        wxUrl.SetString("CFBundleURLName", "weixin");
        wxUrl.SetString("CFBundleURLSchemes", "wx...............");
        PlistElementArray wxUrlScheme = wxUrl.CreateArray("CFBundleURLSchemes");
        wxUrlScheme.AddString( "wx...............");

        plist.WriteToFile(GetInfoPlistPath(buildPath));
    }

 

posted @ 2022-04-01 09:38  kawerd  阅读(897)  评论(0编辑  收藏  举报