UDP Connection Rules for iOS Apps

Are there are “rules” or limitations imposed by Apple regarding UDP connections? I’m asking because my app connects to a UDP group upon startup and this UDP connect is used to synchronize changes between iOS, Desktop and Web apps. It works great when it’s working.

However, sometime after the phone locks or the app is no longer the active app, etc., the UDP connection stops working. I have to force quit the app and restart it in order to get it back. I have code when the app becomes active to check if the socket is connected and if not to connect it. But that doesn’t seem to make a difference. I am never able to duplicate this behavior in the simulator so I can’t debug and see what’s going on.

I know there was an issue when the current mobile framework first came out that UDP sockets would cause the app to crash after unlocking the phone (<https://xojo.com/issue/62875>). But that has been fixed. Still I am wondering if Xojo is somehow dropping the connection but the app still thinks its connected when it’s not.

Or is there some “rule” that Apple imposes preventing apps in the background from being connected to UDP sockets?

Any ideas anyone? I’d love to keep handling UDP packets when in the background but it just doesn’t seem to work well…

I think that in general all iOS are not active at all while in the background with a few exceptions like media (or maybe just audio?) players, some location sensitive apps like navigation (and ride sharing?), etc. I think that there are plist entries for telling iOS you need to run in the background, but I believe there are restrictions to what the App Store allows if those plist entries are used. I’ve not had to research specifics because I do not have the need for background services.

Perhaps you should try an explicit close of the UDP socket when going to the background instead of just trying to ensure it is connected when the app resumes?

I believe what you really need to find out here are iOS rules on background operations more than UDP restrictions.

Edit: Just remembered that iOS now also has options in System Preferences > General > Background App Refresh. You may want to make that the global setting is enabled, and your app enabled. The text on my phone under the global option is:

Allow apps to refresh their content when on Wi-Fi or cellular in the background. Turning off apps may help preserve battery life.

Thanks Douglas. Yes, this is what I was thinking - to explicitly close/destroy the socket when the app stops being active and then rebuild/reconnect it each time the app becomes active. Seems like a waste.

And part of the reason why I’m asking these questions is I am sure there are people on here who know more than I do regarding iOS rules on background operations, etc.

I wish that Apple would have their simulator function with these same set of “rules” for actual devices but that doesn’t seem to be the case…

Perhaps of interest is a Stack Overflow thread from 2020 I found here which references an apparently outdated Apple tech note here which may provide a little more context to a “watchdog” in iOS that can kill off network connections.

However that tech note appears to be from iOS 4 – despite being referenced in a 2020 thread – so this may be providing very outdated “help”.

Edit: I guess that thread is from 2014 – Google had a 2020 date on it…

Well, I’ve added code now to do what I did before Xojo fixed the bug that would cause the crash stated in my first post. So whenever the app gives up active status, the UDP socket is destroyed. Then I re-create it and connect when the app becomes active.

Works much better now…

2 Likes