iOS: Does force quitting the app disables background upload using NSURLSession? - ios

The question is around NSURLSession and NSURLSessionUploadTask.
I'm uploading large files to server and noticed that when I force quit the app the whole background upload just stops. However, when upload starts while app is running through the Xcode/debugger, then my upload completes just fine even when I stop the app running via Xcode 'stop' button. I suspect that force quitting the app using Xcode simulates an iOS system command and not a user action.
So my question is whether it's true that iOS would immediately cancel NSURLSessionUploadTask when user force quit the app?? For some reason I would at least expect an error callback to the app, however nothing happens.

I can confirm now after a bunch of testing that background task will run ok if the app is just put into background. However, if user force quit the app manually, then iOS cancels all scheduled background tasks. So next time the app is launched I'm getting all the callbacks to the delegate with the error code of a canceled task.
Hope it helps someone looking into the same thing.

From Apple's docs about background execution:
If tasks have not yet finished and the system terminates your app, the
system automatically continues managing the tasks in the background.
If the user terminates your app, the system cancels any pending tasks.

Related

How to create a background process on iOS (Swift)?

I want to create a background task to execute some action with a timer, but don't kill that task when app is killed (for example I double click Home button and remove the app from that "list").
Timers don't run after an app is killed in iOS. Killing an app causes the OS to kill all of the apps timers, run loops, and background threads. All your app's executable code is likely deallocated from memory, so is not even around to run.
You could schedule a local (or remote) notification, but that won't execute any code in your app without user interaction. If there is an IFTTT app running on the device, and not killed, perhaps it could be scripted to launch your app periodically.

Background fetch is not working after killing the app

I'm trying to get data from url with background fetch. My func tries to get data and if there is a new data, it sends local notification. Background fetch works after about 10 - 20 minutes when i minimize the app. But when i kill the app (double click home button and close app) it doesn't work. I waited about 1 hour but it didn't work. I am using background service in android and it is working successfully. Is there any way to get data from url and send local notification when app is closed? I am using Xcode 6 and swift 1.2
The Background Fetching will NOT happen in your app after the user has killed it in the multitasking UI. This is by design.
Take a look at Apple's documentation that mentions:
Apps that support background execution may be relaunched by the system
to handle incoming events. If an app is terminated for any reason
other than the user force quitting it, the system launches the app...
and
In most cases, the system does not relaunch apps after they are force
quit by the user. One exception is location apps, which in iOS 8 and
later are relaunched after being force quit by the user. In other
cases, though, the user must launch the app explicitly or reboot the
device before the app can be launched automatically into the
background by the system.
As you noticed in the documentation, there's only 1 exception when the app will be relaunched if it was force killed: by using location services. Unfortunately this does not apply to you.
Also, you cannot detect if the app was force killed by the user. Check this StackOverflow answer.
The Background Fetching will NOT Update in your app after the user has killed app from background. it is not Possible

iPhone indefinite background exectuion Xcode mystery

I am writing an app for personal use that needs continuous background execution. Apple tries to make this difficult, but I found that declaring that my app uses background location updates and then creating background task (with no content, just a blank task) allows my app to run indefinitely when I run the project from Xcode. I do not understand why this works. I noticed that after about three minutes, when presumably the task is supposed to be terminated, I get the following error:
Can't endBackgroundTask: no background task exists with identifier 174256fe0, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
After this message, the app continues to run in the background normally. So, it seems that this indefinite execution is a bit of an exploit, which I'm fine with because I just want to make the app work.
However, when I run the app from the phone (i.e. not pressing the run button in my Xcode project), the background execution seems to stop after 3 minutes. This is a problem, and I can't seem to figure out why execution continues in one case but not in the other. What is the difference between running an app normally and running it through Xcode? Is there a simpler or better way to get the indefinite background execution I'm looking for given that I'm not trying to get this app published to the app store?
Since you're not submitting to the store - why not try this:
Enable the app for background audio services. Then, just play a silent audio file in the background on a constant loop?
The difference is that you are running through Xcode. Normal background limits are removed when running through the debugger.

How to test Background App Launch in case of NSURLSession(Background Session) event?

How can one test the scenario of Application Launch in background for handling Background NSURLSession's event?
Flow:
Application starts a upload/download task using Background URL
session.
User hits home button. App is in suspended or in background
state.
OS decides to Exit the application. I know, one can exit the app by double-tapping home button and swipe-up the particular app. But in that case OS will never re-launch the app in background for event handling.
Upload/download task needs some event handling. OS re-lauches App in background.
So the question is how do I make OS exit the app like it may normally do after some-time. The purpose is to test the code for this scenario. I tried using UIApplicationExitsOnSuspend but it does not work since then App can not be launched in Background.
It's not a perfect solution, but I was able to manually test the launch of an app due to a Background URLSession on a physical device as follows:
Connect device for debugging via USB
Disable Internet connectivity on device (i.e. disable WiFi/Cellular)
Start the app via Xcode
Issue a request using background URLSession. The request shouldn't fail, it will just be waiting around for an Internet connection until it times out, so use a reasonably long timeout to make testing easier.
Kill the app via Xcode (press stop button)
View the device logs via Windows > Devices and Simulators
Enable Internet connectivity on the device again without starting the app
The requests from the background URLSession should then complete and in the device logs (from step 6.) you should see any NSLog statements issued as a result of the app being launched via the application(_:handleEventsForBackgroundURLSession:completionHandler:) app delegate method.
They key point is that killing the app via Xcode, as opposed to killing it using device itself, does not prevent the app from being relaunched for background event handling.
A possible alternative to killing the app manually via Xcode might be to intentionally crash the app in code - this might be more suitable for automated testing.
You could write an app that has a button that allocates and intentionally leaks chunks of memory. If you get this thing to allocate enough RAM, the OS will start killing other apps to get their RAM back.
Hopefully this would exhibit the behavior you need.

Enable download in background with phonegap ios app

I am using phonegap 2.2.0 to develop an ios app. The app requires to download some videos which i'm doing with the FileTransfer. The problem is if the app goes background i.e if the screen is locked or if the home key is pressed, download gets cancelled. I need to continue downloading the videos even when the app is in background. Is there any workaround for this?
The problem is that your app gets suspended after a short period of being in background mode, so it is no longer executing code. You need to either declare and implement your app as a service (UIBackgroundModes in Info.plist), or delay the suspension (in applicationDidEnterBackground handler). Both cases are tricky if you are a pure PhoneGap developer.
I wrote a small PhoneGap plugin for iOS that checks whether the app has called for background mode, and if so prevents it from getting suspended (normally up to 10 minutes) until the mode is revoked.
See this article for more detail.

Resources