How do i handle HTTP load failed (error code: -1009 [1:50]) in swift 4?

我怕爱的太早我们不能终老 提交于 2019-12-07 09:13:36

问题


I created on app using swift 4 and Xcode 9. when I login into my app I send a request and successful login on result which come from json. But when I switch my internet of my phone it crashed and give me this error

HTTP load failed (error code: -1009 [1:50])

So how do I handle this error and give popup or any warning to user to check your internet connection without app crashing.


回答1:


Swift 4, Xcode 10.1 You can access to the error code:

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 {
                ...
            }
        }
    }
}

See also source code at: https://stackoverflow.com/a/53402801/966789



来源:https://stackoverflow.com/questions/49584872/how-do-i-handle-http-load-failed-error-code-1009-150-in-swift-4

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