iOS Question App not responding to socket messages while in background

davemorris

Active Member
Licensed User
Hi all,
I have an app which uses sockets (from the iNetwork library) to communicate with another device over WiFi. Some of the messages received from the other device can cause local notifications to be raised on the iPhone running the app, or the app to send responses back to the other device.

When running the app in debug mode through B4i Bridge, there are no problems – the app will send response messages or raise notifications as expected, no matter if the app is in background, or with the iPhone's screen switched off. However, when I uploaded the app to the iOS App Store and downloaded it from there, I found that the behaviour is very different: whenever the app is not in foreground (e.g. returned to the Home screen, switched to another app, or the phone screen off), the app won't respond at all. Only when the app becomes foreground again, will the expected response/notification occur.

I can't find any reason for this behaviour, and I'm finding it hard to do any in-depth testing as it only occurs in the release version. Is there anything that can be done about the app (or possibly socket?) becoming unresponsive while in background? In B4A, I managed to find a workaround for a similar issue (probably caused by Android's "doze" feature) by setting the relevant Service to be a Foreground Service, but have so far drawn a blank with this problem on iOS.

Thanks,
Dave
 

davemorris

Active Member
Licensed User
The app will be killed a minute or two after it goes to the background. You cannot keep your app running in the background unless it meets Apple specific requirements: https://developer.apple.com/library.../BackgroundExecution/BackgroundExecution.html (see the long running tasks section)

Push notifications might help you.
Thanks for this response. I am looking into push notifications for future versions, as it seems like it might be a more useful way of doing things.

In the meantime, it looks like the 'Background Fetch' mode might be helpful. I found this handy tutorial which looks like I may be able to adapt to my app's needs, so thanks for that.
 
Top