App in Background : task stopped - ios

My app while in background receive pushes to trigger some background tasks, scheduled in a NSOperationQueue.
The first NSOperation terminates correctly, but the second doesn't terminate, it seems like the task is paused, and when I put the app back in foreground the operation can terminate like it should.
Is there restrictions for background tasks ? (The tasks take about 2 or 3 seconds to execute)
Thank you

you have enable any of the background service like Location , Background fetch , Remote Notification to enable active your app in background mode. Please below apple link where you may get more idea -
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Related

Periodic background task on iOS

Is there anyway to run background task on iOS? Like periodically checking some system condition of the phone and report on the widget?
Also, I found that if the iPhone is shut down, then those background task cannot execute again when the phone restart.
The short answer: No.
The long answer is more nuanced, but you shouldn't do it anyway. You can schedule your application for background refresh, but that is unreliable (app will not be run on constant intervals), and if user swipes the app in task switcher, the app will not run at all in the background. Another hacky way is to have a server send a silent push to wake the app more reliably. However this is still not guaranteed to succeed; if user swipes the app in task switcher, your app will not run.
In either case, you cannot trigger widget updates from your app. Widget lifetime is managed by the operating system. Instead, implement widgetPerformUpdate(completionHandler:) and perform your widget updates there. However, as above, this is not a periodic operation.

how to start process in background/Terminated iOS apps

I want to start some process in background mode on timer event.i want to know that is it possible to get start any process in background or terminated apps on timer event,if i set timer for Five minuets and close the app not even running in background mode than after five min process should start automatically, is it possible in iOS?
I have read my Documents and also tried out this Scenario, I have concluded that Apple doesn't provide any background Process while app is terminated excluding Push Notification.
If you want to run any background process then app must be active in background.

NSOperation queue not working in background

I want that my image uploading should work in background.
I have enable Background Modes and made a Operation queue to upload multiple files at a time to server.
But as soon as put app in background NSOperation pauses, and restart again when in foreground.
I have also added beginBackgroundTaskWithExpirationHandler before starting my operation queue and endBackgroundTask when all images get uploaded in server.
This makes all images upload to server also while app is in background, but my app is getting killed, I don't want the app to get killed .
Please suggest some solution to my problem .
Your app has limited time when running in background. After its allotted time has expired it gets killed.
According to Apple:
In your own expiration handlers, you can include additional code needed
to close out your task. However, any code you include must not take too
long to execute because, by the time your expiration handler is called,
your app is already very close to its time limit. For this reason,
perform only minimal cleanup of your state information and end the
task.
Also get the value of the backgroundTimeRemaining property to check the remaining time.

NSURLSession background download - resume over network failure

After reading the Apple documentation about the background download with the new iOS7 api (NSURLSession), I'm a bit disappointed. I was sure that Apple was managing the pause/resume over the network availability in the background (or provide an option to do so) but no…
So reading the documentation, this is what we've got:
https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/NSURLSessionConcepts/NSURLSessionConcepts.html
When any task completes, the NSURLSession object calls the
delegate’s URLSession:task:didCompleteWithError: method with either an
error object, or nil if the task completed successfully.
If the task is a resumable download task, the NSError object’s userInfo dictionary
contains a value for the NSURLSessionDownloadTaskResumeData key. Your
app should use reachability APIs to determine when to retry, and
should then call downloadTaskWithResumeData: or
downloadTaskWithResumeData:completionHandler: to create a new download
task to continue that download. Go to step 3 (creating and resuming
task objects).
So far I understand the solution, but my question is: What architecture is the best to handle the loss of the network and resume downloading in the background?
On my side I'm using reachability and each time the network is available, I resume all tasks (referenced over a NSArray when creating), and suspends them when network is lost. This works well in foreground but for the background I need help on the following points:
If my app has no connectivity in foreground, if I go to the background without connectivity all my tasks remains suspended and won't came back if network is available…
Losing network in background, stop all my downloads/tasks.
Scenario:
In foreground, I start downloading my tasks
I go to background and after 10s switch to "aireplan mode"
All my tasks got an error. So in the method URLSession:task:didCompleteWithError: I resume them using
downloadTaskWithResumeData or if I can't (because some have not
enough resume data) I'm creating a new task without resume-ing it (except if network is back at that time).
Then I put the wifi up
As I'm still in background I cannot trigger a "resume" when network is back without launching the application…
How do I address these points? Have I missed something?
As I'm still in background I cannot trigger a "resume" when network is back without launching the application…
you can use "background fetch",when the app is launched by fetch,then you can check network and resume the download task.
You should create the NSURLSession with background configurations, then your task is sent to a background demon and your app get called when it is completed.
Implementing:
application:handleEventsForBackgroundURLSession:completionHandler:
in the app delegate - without calling the completionHandler - causes the app to hang around in the background after the device loses its connection whilst suspended. That way, the app can still listen to reachability notifications and restart the download when a network connection becomes available once again. However, this is a pretty dodgy approach and may not pass Apple's app store submission guidelines. Additionally, this approach isn't much help when the connection is lost while the app is in the foreground and the connection regained whilst the app is suspended.
In the end I did the following:
Made use of the application:handleEventsForBackgroundURLSession:completionHandler: notification to pause my downloads in the background.
Made use of the intermittent background fetch notification (ie. application:performFetchWithCompletionHandler:completionHandler) to check connection status and restart any paused downloads. (hat-tip #gugupluto)
This still doesn't provide optimal download performance and may lead users to wonder why their "background download" hasn't finished once they reopen the app, but it seems to be the best we can hope for from Apple for now.

Running an application in the Background?

My application runs in the Background (getting location updates) which I need to push to my server for every 10 seconds.
I have scheduled a timer which invokes a function in which the current location updates are captured and pushed to the server. This is running smoothly if the app is there in the foreground. When the app is moved to the background this functionality is running for 15 minutes after which I cannot see the method being invoked at all.
I know if an application is put into background it will be put into suspended state at any time. Also if another app running in the foreground requires memory at that time iOS may terminate some applications in the background. But in my case no application is running in the foreground as I have locked my device.
I also have an idea about expirationHandler. Would like to know if I can keep calling the function in the background without my app going into suspended state and Apple should accept that.
Any suggestions are welcome.
You can add App registers for location updates under Required background modes in your plist.
The same scenario was also in my application i have set the uibackground mode in plist file and use that service from appdeligate and apple approved that application :)
Hope it may help you.

Resources