Check Internet connection availability?

前端 未结 4 1904
悲哀的现实
悲哀的现实 2021-01-17 19:20

I just need to check the internet connection availability before start communicating with the service in my iphone App. I am using Swift 1.2 and Xcode 6 as my development en

4条回答
  •  清酒与你
    2021-01-17 19:50

    An improved version of @Lachezar's answer for Swift2.x:

    Alamofire.request(.POST, url).responseJSON { response in
        switch response.result {
            case .Success(let json):
                // internet works.  
            case .Failure(let error):
    
                if let err = error as? NSURLError where err == .NotConnectedToInternet {
                    // no internet connection
                } else {
                    // other failures
                }
        }
    }
    

    If you would like the network status without parsing an URL, I suggest this solution: https://stackoverflow.com/a/38646078/706933

提交回复
热议问题