Ookey Ookey - 2 months ago 15
Swift Question

Send http requests when app is in background

I have a function which is sending an HTTP request once in 60 sec, but when I lock phone it stops. So, is there a way to send HTTP requests when a phone is locked?
I've tried to do it in

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)


but it's not worked.

Answer

That code seems right to me... It should work as long as the code that you add it in the applicationDidEnterBackground function of your AppDelegate. If the device is locked shouldn't matter from what I've seen. It's still able to perform tasks if the device is locked (ie. play audio)

Also make sure that if you are doing anything in the background that the app has the ability to make Background refreshes, if need be. You can add that in your Capabilities tab under the main project file in Xcode. I think that might be where your issue stems from.