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

苹果在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 

然后添加类似如下的配置:

    NSAppTransportSecurity

   

        NSExceptionDomains

       

            qq.com

           

                NSIncludesSubdomains

               

           

            sina.com.cn

           

                NSIncludesSubdomains

               

           

          

  

根据自己需要的域名修改, NSIncludeSubdomains 顾名思义是包括子域的意思。






你可能感兴趣的:(常见问题解决)