Xcode 7 App Transport Security has blocked a cleartext HTTP 报错解决办法

Xcode 7 创建新项目用到 UIWebView 发送请求时,报下面的错: 
“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内访问网络请求,要采用 HTTPS 协议。

但是现在公司的项目使用的是 HTTP 协议,使用私有加密方式保证数据安全。现在也不能马上改成 HTTPS 协议传输。

最终找到以下解决办法:

1、在Info.plist中添加 NSAppTransportSecurity 类型 Dictionary ;

2、在 NSAppTransportSecurity 下添加 NSAllowsArbitraryLoads 类型Boolean ,值设为 YES;




























































2017/8/3 9:00:17
will-em 2017/8/3 9:00:17


2017/8/3 14:58:17
will-em 2017/8/3 14:58:17


9:16:32
will-em 2017/8/4 9:16:32

    /**
     * 从服务器下载文件
     * @param path 下载文件的地址
     * @param FileName 文件名字
     */
    public static void downLoad(final String path, final String FileName) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //初始化请求对象
                    URL url = new URL(path); //初始化URL对象
                    File file = new File(url.getFile()); //获取此URL的资源名称
                    HttpURLConnection con = (HttpURLConnection) url.openConnection(); //与URL远程对象链接

                    con.setReadTimeout(5*1000);
                    con.setConnectTimeout(500000);
                    con.setRequestProperty("Charset", "UTF-8");
                    con.setRequestProperty("Connection", "Keep-Alive");
                    con.setRequestMethod("GET");
                    long length =  con.getContentLength();
                    if (con.getResponseCode() == 200) {
                        InputStream is = con.getInputStream();//获取输入流
                        FileOutputStream fileOutputStream = null;//文件输出流
                        if (is != null) {
                            FileUtils fileUtils = new FileUtils();
                            fileOutputStream = new FileOutputStream(fileUtils.createFile(FileName));//指定文件保存路径,代码看下一步
                            byte[] buf = new byte[1024];
                            int ch;
                            while ((ch = is.read(buf)) != -1) {
                                fileOutputStream.write(buf, 0, ch);//将获取到的流写入文件中
                            }
                        }
                        if (fileOutputStream != null) {
                            fileOutputStream.flush();
                            fileOutputStream.close();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值