NSData cannot retrive image from internet anymore [duplicate]

若如初见. 提交于 2019-12-04 06:52:32

问题


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)!)

回答1:


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.

To Allow all http domains from your application, you should add

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

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.




回答2:


With iOS 9 you can't call an HTTP anymore because the ATS (App Transport Security) calls should be HTTPS. To work with HTTP links, you should insert the following key in the info.plist file to disable the ATS:

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

create a new voice in the info.plist file "NSAppTransportSecurity" like dict. insert in it the key "NSAllowsArbitraryLoads" like boolean and set to YES

;-)



来源:https://stackoverflow.com/questions/32820320/nsdata-cannot-retrive-image-from-internet-anymore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!