我正在尝试使用有效的 URL 从 iTunes 下载图像 - 但未下载该图像。这是链接:
现在,当我尝试下载图像时,由于某些奇怪的原因,它返回 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 中,Apple 开始要求 URL 具有应用程序传输安全性。这意味着“http:”需要替换为“https:”。
当您获得像 artsUrl60 这样的图像 URL 时,它仍然显示为“http:”,因为 Apple 不想破坏现有的应用程序。
合乎逻辑的做法是将“http:”替换为“https:”。
但这行不通!如果将其粘贴到浏览器中,它甚至不起作用,因为 mzstatic.com 网站似乎没有有效的证书。
解决方案是完全关闭 ATS(不推荐)或将 mzstatic.com 列入白名单。
打开 Info.plist 并插入以下内容:
<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>
这似乎解决了问题。
大卫
关于ios - 无法从 iTunes 下载图像作品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32850077/