Xcode iOS application Swift 3 Stop listening when I leave application window and go out to home screen

I have an issue with my iOS app, written on Swift 3 with Xcode.


I'm using WiFi Socket server Communication in my app to communicate with a Raspberry Pi via WiFi protocol, and UserNotification in Xcode to receive these notification sent from RPi.


The problem is when I leave the application's window and go out to home screen, it seems my app stop to listening and when my RPi send the nothifications, there is no notification recived on my iPhone.


When I test my application with Xcode simulator, it works well, on applications's window or out there on home screen,

But when I test it on my iPhone device, it receive the notifications only when I rest on application's window, and stop working when I push the home's button on my iPhone to being on the home screen, or any where outside of application's windows.


Maybe someone Know a solution,

Thanks in advance,

The problem is when I leave the application's window and go out to home screen, it seems my app stop to listening …

Right. When your app moves to the background it typically becomes eligible for suspension. In most cases it will be suspended shortly thereafter, and thus unable to service network requests. To further add to the fun, under some circumstances the system will reclaim resources out from underneath your sockets. You can read Technote 2277 Networking and Multitasking to learn more about this.

How you deal with this depends on your user-level goals. For example:

  • If you only need to interact with the device while your app is in the front, you can just disconnect from the device when you move to the background and reconnect when you come back to the front.

  • If it’s only need to interact with the device for short periods of time after moving to the background, you can use a UIApplication background taks to prevent your app from being suspended for a few minutes.

  • If you need persistent, long-time access to the device while in the background, you will need to rethink your approach. There’s no supported way to achieve this on iOS.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Xcode iOS application Swift 3 Stop listening when I leave application window and go out to home screen
 
 
Q