美文网首页
[bug]iOS不能正常使用 Http 的解决方法

[bug]iOS不能正常使用 Http 的解决方法

作者: codeTao | 来源:发表于2018-07-27 14:05 被阅读271次

    输出的错误信息如下:

    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
    
    
    sk666.png

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

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

        <key>NSAppTransportSecurity</key>
        <dict>
            <key>NSExceptionDomains</key>
            <dict>
                <key>qq.com</key>
                <dict>
                    <key>NSIncludesSubdomains</key>
                    <true/>
                </dict>
                <key>sina.com.cn</key>
                <dict>
                    <key>NSIncludesSubdomains</key>
                    <true/>
                </dict>
              </dict>
         </dict>
    
    

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

    NSIncludeSubdomains 是否包括子域;

    相关文章

      网友评论

          本文标题:[bug]iOS不能正常使用 Http 的解决方法

          本文链接:https://www.haomeiwen.com/subject/xkzwmftx.html