'Detect active internet connection with "Network" framework in Swift

I am using "Network" framework to constantly listen to network changes in my application.

Here is the code for reference.

import Network

public class NetworkService {
    public static let instance = NetworkService()
    private let queue = DispatchQueue(label: "NetworkService")
    private let monitor = NWPathMonitor()
    private init() {
        monitorNetwork()
    }
    
    func monitorNetwork() {
        monitor.pathUpdateHandler = { [weak self] path in
            
            if path.status != .satisfied {
                print("Disconnected")
            }
            else {
                Print("Connected")
            }
        }
        monitor.start(queue: queue)
    }
}

I tested this in real device and it works for the most part. My issue is with one use case.

My issue - I need to know when the iPhone is connected to the wifi with working internet connection.

Here is what I tried (observed).

When the iPhone connects to a wifi (with internet access and without internet access) in both cases callback gives me "path.status" as satisfied.

But "path" object is not same in both cases. I am providing the callback response below.

"path" object, when iPhone connects to Wifi with no internet access.

 satisfied (Path is satisfied), interface: en0, ipv4, dns
  - status : Network.NWPath.Status.satisfied
  ▿ availableInterfaces : 1 element
    ▿ 0 : en0
      - type : Network.NWInterface.InterfaceType.wifi
      - name : "en0"
      - index : 14
      - nw : en0
  - isExpensive : false
  - supportsIPv4 : true
  - supportsIPv6 : false
  - supportsDNS : true
  ▿ internalGateways : 1 element
    ▿ 0 : **.**.**.**
      ▿ hostPort : 2 elements
        ▿ host : **.**.**.** 
          ▿ ipv4 : **.**.**.**
            ▿ address : in_addr
              - s_addr : 19638464
            - interface : nil
        ▿ port : 0
          - port : 0
  - localEndpoint : nil
  - remoteEndpoint : nil
  ▿ nw : Optional<OS_nw_path>
    - some : satisfied (Path is satisfied), interface: en0, ipv4, dns

"path" object when iPhone is connected to Wifi with internet access.

▿ satisfied (Path is satisfied), interface: en0, ipv4, ipv6, dns
  - status : Network.NWPath.Status.satisfied
  ▿ availableInterfaces : 1 element
    ▿ 0 : en0
      - type : Network.NWInterface.InterfaceType.wifi
      - name : "en0"
      - index : 14
      - nw : en0
  - isExpensive : false
  - supportsIPv4 : true
  - supportsIPv6 : true
  - supportsDNS : true
  ▿ internalGateways : 2 elements
    ▿ 0 : **.**.**.** 
      ▿ hostPort : 2 elements
        ▿ host : **.**.**.**
          ▿ ipv4 : **.**.**.**
            ▿ address : in_addr
              - s_addr : 19638464
            - interface : nil
        ▿ port : 0
          - port : 0
    ▿ 1 : **.**.**.** 
      ▿ hostPort : 2 elements
        ▿ host : **.**.**.** 
          ▿ ipv6 : **.**.**.** 
            ▿ address : in6_addr
              - __u6_addr : __C.in6_addr.__Unnamed_union___u6_addr()
            ▿ interface : Optional<NWInterface>
              ▿ some : en0
                - type : Network.NWInterface.InterfaceType.wifi
                - name : "en0"
                - index : 14
                - nw : en0
        ▿ port : 0
          - port : 0
  - localEndpoint : nil
  - remoteEndpoint : nil
  ▿ nw : Optional<OS_nw_path>
    - some : satisfied (Path is satisfied), interface: en0, ipv4, ipv6, dns

Please notice difference. In second case (with working internet one) ipv6 support is marked true, but it is false when Wifi has no internet access. [I repeated this observation multiple times it's always the same].

Coming to the question.

Can I base my solution on this observation that if ipv6 support is true, then iPhone is connected to active internet connection?

Or, is there a better way to know that Wifi is connected with working internet access?

Advance apology if question is a duplicate. I tried to find - None is having the clarity I need.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source