我在应用程序上使用swift 4和xcode 9创建。当我登录到应用程序时,我会发送请求并成功登录json的结果。但是,当我切换手机互联网时,它崩溃了,给我这个错误
http负载失败(错误代码:-1009 [1:50](
因此,如何处理此错误,并向用户发出任何警告,以检查您的Internet连接而不会崩溃。
swift 4,xcode 10.1 您可以访问错误代码:
class ViewController1: UIViewController, URLSessionDataDelegate {
...
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if error != nil {
debugPrint("error message: (error!)")
debugPrint("code: (error!._code)")
if error!._code == -1009 {
...
}
}
}
}
另请参见源代码:https://stackoverflow.com/a/53402801/966789