Xcode7创建的工程发送HTTP请求报错

来源:互联网 发布:会java学php要多久 编辑:程序博客网 时间:2024/05/22 10:50
苹果在Xcode7之后,在创建新项目用到 URL 发送请求时,对协议进行了限制,开始采用HTTPS协议
所以在创建工程时若需要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”

解决方案:
1、在Info.plist中添加 NSAppTransportSecurity 类型 Dictionary ;
2、在 NSAppTransportSecurity 下添加 NSAllowsArbitraryLoads 类型Boolean ,值设为 YES;   

3、注意:单元测试下有一个info.plist,修改那个文件是木有作用的!


其他

如果想让有的域名支持https的话,可以这么配置: 

对于实在不支持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顾名思义是包括子域的意思。






0 0