Web connectivity in iOS – How you can verify – iOSTutorialJunction



Swift Tutorials Detect change in internet connection using Network framework

Earlier we use Reachability courses to verify for web connectivity in iOS sdk. In swift, we generally use cocoa-pod named Reachability to observe for community modifications. With iOS 12.0, apple present a brand new framework named Community framework. In Community framework, we’ve one class named NWPathMonitor that monitor for community connectivity in iOS sdk.

On this publish, we’ll discover ways to monitor for community modifications utilizing NWPathMonitor of Community framework. Comply with the beneath steps to verify for web connectivity in iOS utilizing swift language.

First, import Community framework

Subsequent, we’ll create a perform named monitorNetwork. This perform can be accountable for monitoring modifications in community connection for our iOS app.

Now create an occasion of NWPathMonitor.

func monitorNetwork() {
      let monitor = NWPathMonitor()

The NWPathMonitor has two kind’s of initializer’s

  • First one, has no parameters indicating the we have to monitor any kind of web connectivity. Both mobile community or WiFi connectivity.
  • Second initialize’r, has a parameter named requiredInterfaceType. This give developer flexibility to observe for a specific kind of community connection.

Forms of InterfaceType in Community framework

 public enum InterfaceType {

        /// A digital or in any other case unknown interface kind
        case different

        /// A Wi-Fi hyperlink
        case wifi

        /// A Mobile hyperlink
        case mobile

        /// A Wired Ethernet hyperlink
        case wiredEthernet

        /// The Loopback Interface
        case loopback

Detect web connectivity in iOS

To detect change in web connectivity utilizing community framework, we’ll hear for community occasions present by iOS on pathUpdateHandler. Right here we’ll detect if standing of path, returned in pathUpdateHandler is staisfied or not. If path is glad which means we’ve lively web connection and if path is just not glad then there is no such thing as a community connectivity. Lastly, we’ll add monitor to background queue.

   func monitorNetwork() {
        let monitor = NWPathMonitor()
        monitor.pathUpdateHandler = { path in
            if path.standing == .glad {
                print("Intenet related")
            } else {
                print("No web")
        let queue = DispatchQueue(label: "Community")
        monitor.begin(queue: queue)

The place to go from right here

On this publish we realized, easy methods to detect for lively web connection in swift utilizing community framework. Nonetheless, Community framework is simply out there from iOS 12.0 and above. So in case your app can also be supporting iOS decrease than iOS 12.0. Then you have to search for Reachability pod, in order that one can verify for web connectivity in iOS app utilizing swift language.