NSData无法再从互联网上检索图像 [英] NSData cannot retrive image from internet anymore
问题描述
以下代码在iOS8.4.1(包括8.4.1)之前正常工作。虽然它在iOS9.0.1中没有任何问题。这是一个错误还是公众宣布这一变化?我测试了两台iPad。
The following code works fine before iOS8.4.1 (includ 8.4.1). While it ruturns nil in iOS9.0.1. Is it a bug or there is a public annoucement for this change? I tested with two iPads.
let url = NSURL(string: "http://www.mapshots.com/wp-content/uploads/2014/05/mapshots-ag-studio-agricultural-mapping-software-logo.png")
let data = NSData(contentsOfURL: url!)
NSLog("Data length @%", (data?.length)!)
推荐答案
这是与Apple在iOS 9中进行的ATS(应用程序传输安全协议)更改相关的问题。默认情况下,iOS9忽略与 http
协议的通信。您的网址应为 https
。但是,您可以在应用中包含特定域的例外,或者允许在您的应用中允许所有 http
通信。
检查文档以获取完整的详细信息。
This is an issue related to ATS(App Transport Security Protocol) changes made by Apple in iOS 9. By default iOS9 disregard communication with http
protocol. Your URL should be https
. However you can include exception for specific domains in your app or you can allow all http
communication to be allowed from within your app.
Check the Documentation for full details.
要允许应用程序中的所有http域,您应该添加
To Allow all http domains from your application, you should add
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
但是,由于Apple推荐了这些新设置,您应该选择在此特定域中添加例外应用而不是允许所有 http
域。查看此线程来实现这一目标。
But as Apple has recommended these new settings, you should chose to add exception for this specific domain in your app rather than allowing all http
domains. Check this thread to achieve this.
这篇关于NSData无法再从互联网上检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!