NSData contentsOfUrl返回nil [英] NSData contentsOfUrl returns nil

查看:89
本文介绍了NSData contentsOfUrl返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow上发现了几个类似的问题,但没有一个能解决我的问题。

I found several similar questions on StackOverflow but none of them solve my problem.

我试图从网址获取图片。我是这样做的:

I am trying to get a image from a url. Here's how I do it:

let url = NSURL(string: "http://pic3.zhimg.com/8161ba9638273e0fb1a0201789d22d8e_m.jpg")
let data = NSData(contentsOfURL: url!)
let image = UIImage(data: data!)

但我告诉我数据为零时出错。

But I got an error telling me that data is nil.

我该如何解决这个问题?谢谢。

How can I solve this? Thanks.

更新

以下是错误的一些屏幕截图:

Here's some screenshots of the error:


推荐答案

这可能是Apple新的应用程序传输安全性拒绝非HTTPS请求的结果。要解决此问题,您需要修改应用程序的 Info.plist 文件。您可以为该特定域定义例外

This is probably a result of Apple's new app transport security denying a non-HTTPS request. To work around this you need to modify your app's Info.plist file. You can either define an exception for that particular domain

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>pic3.zhimg.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

或完全禁用ATS

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

这篇关于NSData contentsOfUrl返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆