Friday, December 8, 2023
HomeiOS DevelopmentWeb connectivity in iOS – The right way to examine – iOSTutorialJunction

Web connectivity in iOS – The right way to examine – iOSTutorialJunction

Swift Tutorials Detect change in internet connection using Network framework

Earlier we use Reachability lessons to examine for web connectivity in iOS sdk. In swift, we generally use cocoa-pod named Reachability to watch 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. Observe the beneath steps to examine for web connectivity in iOS utilizing swift language.

First, import Community framework

Subsequent, we’ll create a operate named monitorNetwork. This operate might 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 watch for a specific kind of community connection.

Sorts 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 happy which means we’ve lively web connection and if path just isn’t happy 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 == .happy {
                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 discovered, easy methods to detect for lively web connection in swift utilizing community framework. Nevertheless, Community framework is barely obtainable from iOS 12.0 and above. So in case your app can also be supporting iOS decrease than iOS 12.0. Then you must search for Reachability pod, in order that one can examine for web connectivity in iOS app utilizing swift language.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments