IOS9 不能正常使用 Http 的解决方法

前段时间做项目的时候遇到 http 不能使用的情况,特此总结一下解决方法。

输出的错误信息如下:

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.

官方文档是这样解释的:

App Transport Security (ATS) enforces best practices in the secure connections between an app and its back end. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt; it is also on by default in iOS 9 and OS X v10.11. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one.
If you’re developing a new app, you should use HTTPS exclusively.
If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible. In addition, your communication through higher-level APIs needs to be encrypted using TLS version 1.2 with forward secrecy. If you try to make a connection that doesn't follow this requirement, an error is thrown. If your app needs to make a request to an insecure domain, you have to specify this domain in your app'sInfo.plistfile.

注 : App Transport Security (ATS) 是 IOS9 中引入的新特性

以上用一句话概括就是: 新特性要求 App 内访问的网络必须使用HTTPS协议

解决方案

1.在 Info.plist 中添加NSAppTransportSecurity类型Dictionary。
2.在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean, 值设为YES
IOS9 不能正常使用 Http 的解决方法_第1张图片
Paste_Image.png

以上方法虽然解决了 http 不能正常使用的问题,但是苹果提供的安全保障也被关闭了, 对于不支持 https 协议的应该首先考虑 例外

方法如下:
右键 Info.plist 选择 open with source code
添加类似如下配置:

    NSAppTransportSecurity
    
        NSExceptionDomains
        
            qq.com
            
                NSIncludesSubdomains
                
            
            sina.com.cn
            
                NSIncludesSubdomains
                
            
          
     

: 根据自己需要的域名修改

NSIncludeSubdomains 是否包括子域;

你可能感兴趣的:(IOS9 不能正常使用 Http 的解决方法)