Unity 通过 www 下载 assetbundle , 在 iOS9 设备无法下载的问题

我们项目是通过 www 下载 Assetbundle 来实现热更新的, 在 iOS 8上一切正常,但在 iOS9 设备上发现无法下载,跟踪调试发现以下错误信息

“App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.”

原因是 iOS9 引入了新的 App Transport Security (ATS) 特性,禁止使用 http 协议传输。

但是我们可以通过修改 pList 文件来绕过这个问题:

当我们通过 Unity 导出 xCode 工程时,如果采用的是 "Replace" 模式, 则需要我们手动设置 pList 文件,

这样会非常麻烦,幸好 Unity 提供了修改 pList 文件的接口,自动设置相关代码如下:

 1 using UnityEngine;
 2 using UnityEditor;
 3 using UnityEditor.Callbacks;
 4 using System.Collections;
 5 using UnityEditor.iOS.Xcode;
 6 using System.IO;
 7 
 8 public class ChangPlistSetting {
 9     
10     [PostProcessBuild]
11     public static void ChangeXcodePlist(BuildTarget buildTarget, string pathToBuiltProject) {
12         
13         if (buildTarget == BuildTarget.iOS) 
14         {    
15             // Get plist
16             string plistPath = pathToBuiltProject + "/Info.plist";
17             PlistDocument plist = new PlistDocument();
18             plist.ReadFromString(File.ReadAllText(plistPath));
19             
20             // Get root
21             PlistElementDict rootDict = plist.root;
22             
23             // Add value of NSAppTransportSecurity in Xcode plist
24             var atsKey = "NSAppTransportSecurity";
25 
26             PlistElementDict dictTmp = rootDict.CreateDict( atsKey );
27             dictTmp.SetBoolean( "NSAllowsArbitraryLoads", true);
28                         
29             // Write to file
30             File.WriteAllText(plistPath, plist.WriteToString());
31 
32         }
33     }
34 }

将该文件放入 Editor 目录下即可。

转载于:https://www.cnblogs.com/marrco2005/p/5016218.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值