最近写的一些Demo很多都有带有http的URL,然而在Xcode V7.0以及更高版本下会提示报错。
报错提示1:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
报错提示2: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.
不管是使用Xcode编写iOS程序还是Mac OS X程序都是会报错误的,但是,报错是指控制台输出,程序不会crash退出,但是你的网络访问是失败的,不会得到数据返回。
经查证,是iOS引入新特性App Transport Security (ATS) 导致:
新特性要求App内访问网络需要使用HTTPS协议,但是当前很多链接都是HTTP,使用私有加密方式保证数据安全。
解决方法:
如下为网上提供方法:
1.在Info.plist中添加NSAppTransportSecurity
类型Dictionary
。
2.在NSAppTransportSecurity
下添加NSAllowsArbitraryLoads
类型Boolean
,值设为YES
我在项目中的info.plist文件添加键值对的时候,NSAppTransportSecurity会自动变成App Transport Security Settings,
-
NSAllowsArbitraryLoads会变成Allow Arbitrary Loads
只要您能这么添加就对了,如图: