我正在尝试从iTunes下载具有有效URL的图像-但图像未被下载。这是链接:
http://is5.mzstatic.com/image/thumb/music7/v4/53/fc/a2/53fca253 - 84 - b1 - f2cd - 4 - e17 - 98 be502ec53c/umg_cvrart_00602547534873_01_rgb72_1500x1500_15umgim41882.jpg/60x60bb - 85. - jpg
现在当我尝试下载图像时,由于一些奇怪的原因它返回NULL:
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://is5.mzstatic.com/image/thumb/Music7/v4/53/fc/a2/53fca253-84b1-f2cd-4e17-98be502ec53c/UMG_cvrart_00602547534873_01_RGB72_1500x1500_15UMGIM41882.jpg/60x60bb-85.jpg"]];
这只发生在iTunes链接的艺术作品。
(lldb) po imageData
nil
我知道这是一段时间,但如果其他人正在寻找解决方案…
在iOS 9中,苹果开始对url要求应用传输安全。这意味着"http:"需要替换为"https:"。
当你得到一个像artworkUrl60这样的图片URL时,它仍然是"http:",因为苹果不想破坏现有的应用程序。
合乎逻辑的做法是将"http:"替换为"https:"。
但这行不通!如果你将它粘贴到浏览器中,它甚至无法工作,因为mzstatic.com网站似乎没有有效的证书。
解决方案是完全关闭ATS(不推荐)或将mzstatic.com列入白名单。
打开信息。选择并插入以下内容:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSExceptionDomains</key>
<dict>
<key>mzstatic.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
问题似乎解决了。
大卫