【发布时间】:2015-12-17 17:10:28
【问题描述】:
我正在使用 NSURL 从网站上抓取 HTML。问题是,如果在 NSURL 请求期间互联网中断,应用程序就会崩溃。
代码:
let myUrl = NSURL(string: "http://www.mywebsite.com/page.html")
let request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
if error != nil {
print("Error: \(error)")
}
dispatch_async(dispatch_get_main_queue()) {
self.testLabel.text = "\(responseString!)"
}
}
}
task.resume()
我可以使用此代码在 NSURL 连接之前检查互联网连接,但是在操作过程中仍有可能断开互联网:
在 NSURL 之前检查互联网连接的代码:
let myUrl = NSURL(string: "http://www.mywebsite.com/page.html")
let request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
if error != nil {
print("Error: \(error)")
}
dispatch_async(dispatch_get_main_queue()) {
self.testLabel.text = "\(responseString!)"
}
}
}
task.resume()
为了解决这个问题,我使用 try/catch 操作进行了研究,但是我没有成功构建一个不会给我错误的操作。有没有办法将整个 NSURL 操作包装在一个巨大的 try/catch 中以捕获任何错误?我在想一些类似的事情,但它不起作用:
代码:
let myUrl = NSURL(string: "http://www.mywebsite.com/page.html")
let request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
if error != nil {
print("Error: \(error)")
}
dispatch_async(dispatch_get_main_queue()) {
self.testLabel.text = "\(responseString!)"
}
}
}
task.resume()
} catch {
timer.invalidate
sendAlert("Error", message: "You are not connected to the internet")
}
【问题讨论】:
-
看起来你想要像Reachability这样的东西
-
使用保护解开数据 let data = data where error == nil else { return }
-
在检查错误之前,您正在对
data(例如data!)进行强制解包。除非你知道它永远不可能是nil,否则永远不要强制展开。否则你会崩溃。因此,正如 Leo 建议的那样,添加一些错误处理以确保data不是nil并且error是nil。
标签: ios swift nsurlsession