I am trying to obtain NSData from a server using contentsOfURL. When the iPhone is connected to wifi, it works fine. But when not connected to wifi, my app crashes with an EXC_BREAKPOINT message. How can I handle or circumvent this issue of crashing when not connected?
do {
let varOption = try NSData(contentsOfURL: NSURL(string: urlToRequest)!, options:NSDataReadingOptions.DataReadingMappedIfSafe)
} catch {
print("error encountered")
}
Is there a requirement to use NSData
's contentsOfURL
? Why not make an async web request using NSURLSession
?
NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: urlToRequest)!) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
// if data && !error, do the thing
}.resume()