Check Internet connection availability?

天大地大妈咪最大 提交于 2019-12-01 15:55:24

I was looking to solve the same problem for myself this morning and I felt that this "Reachability" sample code provided from Apple does not appeal to me at all (mostly because of the manual opening of sockets and the SystemConfiguration ugly API).

Instead of this I tried to make a simple HTTP connection to a random non-existing URL using Alamofire (I already had it as a dependency) and check the value in the error that I would receive.

Alamofire.request(.GET, "http://superrandomdomainnamethatisnotused.com/superrandompath").responseString {
    (request, response, stringData, error) in

    if let networkError = error {
        if (networkError.code == -1009) {
            println("No Internet \(error)")
        }
    }
}

This code could be rewritten using any other networking library. Error -1009 is corresponding to NSURLErrorNotConnectedToInternet which is somewhat more reassuring way of saying "you are really not connected to the Internet".

Another good thing is that this works even if you put non-existing URL, which means that you don't have to make a successful HTTP request to any server even if you have Internet connection.

The downside is that the code in the block is executed asynchronously, which could be inconvenience depending on your requirements.

Melvin

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

Alamofire

I would recommend listen to reachability instead of using a get api.

You can add following class to your app, and call MNNetworkUtils.main.isConnected() to get a boolean on whether its connected or not.

class MNNetworkUtils {
  static let main = MNNetworkUtils()
  init() {
    manager = NetworkReachabilityManager(host: "google.com")
    listenForReachability()
  }

  private let manager: NetworkReachabilityManager?
  private var reachable: Bool = false
  private func listenForReachability() {
    self.manager?.listener = { [unowned self] status in
      switch status {
      case .notReachable:
        self.reachable = false
      case .reachable(_), .unknown:
        self.reachable = true
      }
    }
    self.manager?.startListening()
  }

  func isConnected() -> Bool {
    return reachable
  }
}

This is a singleton class. Every time, when user connect or disconnect the network, it will override self.reachable to true/false correctly, because we start listening for the NetworkReachabilityManager on singleton initialization.

Also in order to monitor reachability, you need to provide a host, currently I am using google.com feel free to change to any other hosts or one of yours if needed.

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