Діма Комар
Діма Комар

Reputation: 552

How to check when ios device just lost connection to internet

how to detect losing connection to network as fast as it can be

now im using reachabitity framework for swift and it takes abouut 3 sec to detect that there is no network so user can make the app crash

here how i check it :

func setupReachability() {
    do {
        self.reachability = try Reachability.reachabilityForInternetConnection()
    } catch {
        print("Cannot setup reachability monitoring")
        return
    }

    self.reachability!.whenReachable = { reachability in
        self.loginButton(true)
    }
    self.reachability!.whenUnreachable = { reachability in
        self.loginButton(false)
    }

    do { try self.reachability!.startNotifier() } catch {
        print("Cannot start reachability monitoring")
        return
    }

    print("Started reachability")
}

Upvotes: 0

Views: 1082

Answers (1)

gnasher729
gnasher729

Reputation: 52632

You always, ALWAYS, must handle errors in your connections. You absolutely cannot rely on Reachability. You also cannot rely that your connection goes to the server that you want to connect to. It is possible and practically happens a lot that you get a reply from a totally different server in a totally different format than you expected, and you need to handle that. (Take your app to the nearest Starbucks and check whether it survives).

Upvotes: 1

Related Questions