As noted here, https://developer.apple.com/forums/thread/116799 the Network framework probably won't have a connection available when running in the background.
We've been using the BGTask for a couple years now to start a URLSession and pull data from a web server. It works very nicely and reliably.
Do we have any options if we want to connect to another iPad, though? I ran a test and even if I have a "server" iPad running a Network framework listener (NWListener), and the app is in the foreground and the screen on, a "client" iPad (NWBrowser) cannot connect to the NWListener when trying to connect from the BGTask; it gives a DefunctConnection error.
Why does the Network framework not have the network available to it, but a URLSession does?
Is this a limitation of the iPad, or the Network framework? If I had an iPad running as a web server like this project,
https://github.com/swisspol/GCDWebServer
and an iPad client tries to connect a URLSession to it, would that work?
If this is an iPad limitation, could I use a MacBook on the network as a web server and connect to that instead?
The task will definitely be running from a
BGTask
at night, which means the app will be suspended (and therefore also backgrounded).
Huh? A BGTask
running at night is most likely a background processing task. Such a task keeps your app running in the background for the duration of the task, meaning you’re free to use any networking API.
Just make sure you set the requiresNetworkConnectivity
property in your request.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"