ios开发:访问http协议而非https协议的地址时报错
一,报错信息:
[Result]: failure(Alamofire.AFError.sessionTaskFailed(error: Error Domain=NSURLErrorDomain Code=-1022
"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."
二,原因
报错 Code=-1022
是因为从 iOS 9 开始,苹果默认开启了 ATS (App Transport Security),
要求所有网络请求必须使用 HTTPS 协议,而你正在尝试访问普通的 HTTP 地址。
要解决这个问题,你需要修改项目的 Info.plist 文件来允许 HTTP 访问
三,解决:
在info页面:
点击加号添加一行
添加选 App Transport Security Settings 这是一个数组(此时没有元素)。
再往该数组中添加一个 Allow Arbitrary Loads 元素,同时设为 YES。
这样就可以在项目中使用http协议进行网络请求了。
如下图:

浙公网安备 33010602011771号