4

I am developing an iOS app and I want to detect when the user connects/disconnects to Wifi even when the app is closed. I did a lot of research, but still didn't find any solutions to this problem.

Can someone point me in the general direction of how to do this?

Benjamin Lowry
  • 3,730
  • 1
  • 23
  • 27
Jordy
  • 1,036
  • 8
  • 9
  • Consider looking at these links for some help for detecting wifi connection. If I'm not mistaken, it's not possible. http://stackoverflow.com/questions/30743408/check-for-internet-connection-in-swift-2-ios-9 http://stackoverflow.com/a/13638550/6448167 – KSigWyatt Oct 11 '16 at 15:11
  • Also, If you'd like to detect anything however ensure that you include the code in `Application: didEnterBackground` in your AppDelegate – KSigWyatt Oct 11 '16 at 15:15
  • Hi Jordy, do you got some idea about this? – dianyi Feb 01 '18 at 14:54

3 Answers3

2

It is not possible to detect network connection after the application is closed. The process is shut down and your code cannot be executed.

Check iOS application lifecycle for more details

Summer
  • 488
  • 4
  • 14
  • You're definitely right if the app is quit. The question is whether or not OP really means that when he says "closed" as that is more ambiguous. Either way, +1 for accuracy. – Benjamin Lowry Oct 11 '16 at 15:38
  • @BenjaminLowry Thanks. That is exactly why I recommended OP to read more about Application Lifecycle. OP need to understand the difference between "Suspended", "Background" and "Not running" in order to figure out what to achieve and describe it with more precise terminology. – Summer Oct 11 '16 at 15:46
0

Maybe you should considered option of application, that can run in background. This is of course possible in iOS, you need Capability type:Background Modes. Then you can check if wifi is availible.

MiXen
  • 105
  • 1
  • 2
  • 12
0

For some unspecified time you can attain this by using Background Fetch.

override init() {
    super.init()
    initializeBackgroundTask()
    NotificationCenter.default.addObserver(self, selector: #selector(networkHasChanged(notification:)), name: NSNotification.Name.reachabilityChanged, object: nil)
}

func networkHasChanged(notification : NSNotification) {
    if let reachability = notification.object as? Reachability {
             // Do whatever you want to do!!!
    }
}

func initializeBackgroundTask() {
    if bgTask == UIBackgroundTaskInvalid {
        bgTask = UIApplication.shared.beginBackgroundTask(withName: "CheckNetworkStatus", expirationHandler: {
            self.endBackgroundTask()
        })
    }
}


func endBackgroundTask() {
    if deepLinkString == nil {
        if (self.bgTask != UIBackgroundTaskInvalid) {
            UIApplication.shared.endBackgroundTask(self.bgTask)
            self.bgTask = UIBackgroundTaskInvalid
        }
    }
}

deinit {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.reachabilityChanged, object: nil)
}

Also try not to initialise background task if not in use.

niku
  • 472
  • 3
  • 12