swiftinternet-connection

One time Internet Connection Check swift


I need to check the internet connection of a user for one time.

I thought of something like Device.isConnectedToTheInternet but I didn't find something similar... Every solution that if found was like an internet monitor over time, but that's not exactly what I need.

Someone ideas? Thanks, Boothosh


Solution

  • As Rob Napier mentioned, it looks like the only thing readily available is the NWPathMonitor, which you can use to check the 'connectivity' to a certain location, like www.google.com, or something else that would be relatively trustworthy to be available 24/7, or just the URL you are about to try and use. Here is a short HackingWithSwift tutorial for NWPathMonitor.

    I know this isn't exactly your current situation, but in my projects (and a lot of peoples projects) using an API like AlamoFire is pretty common, and has a ton of usefulness for creating GET/PUT/POST/DELETE requests and of course, has a NetworkReachabilityManager which can be used to make a convenient global function for a simple true/false result for 'isConnectedToInternet' like this:

     /*
     Connectivity
     Struct
     Utilizes AlamoFire to check for network availability.
     isConnectedToInternet should return true in all cases the phone has access (Cellular, No cellular + wifi, Cellular + wifi, wifi)
    */
    struct Connectivity {
        static let sharedInstance = NetworkReachabilityManager()!
        static var isConnectedToInternet:Bool {
            let connected = self.sharedInstance.isReachable
            return connected
        }
    }//end Connectivity