iosswiftreachability

Why does Network library fails to detect when internet is connected back in Swift Ios


I have the following code to detect my internet connection state

  override func viewDidLoad() {
    super.viewDidLoad()
    internetStatus()
    
    // Do any additional setup after loading the view.
    
 }
 func internetStatus(){

    let nwPathMonitor = NWPathMonitor()
    nwPathMonitor.start(queue: DispatchQueue(label: "network_monitor"))
    nwPathMonitor.pathUpdateHandler = { path in
       
        if (path.status == .satisfied){
            if (path.usesInterfaceType(.wifi)){
                print("wifi")
            }
            if (path.usesInterfaceType(.cellular)){
                print("cellular")
            }
        }
        if (path.status == .unsatisfied)
        {
            print("noConnection")
        }
        if (path.status == .requiresConnection){
            
        }
    }
}

This works fine but on iOs simulator I am unable to detect reconnects. When I disconnect "noConnection" is printed which fine but when I connect "noConnection" prints again. Is this some sort of bug or there is something wrong in my code


Solution

  • So this works fine on real device but on simulator it doesnot work. I am using macos Big Sur and XCode 12.5