Imagine i am setting up a location background task, for my iOS app, to track the users location. The user granted my app access to his location "Always". Does this mean, the background tasks runs only when started from the app, until the phone ist shut down, or does this mean, that the location background task is restarted after rebooting the os?
It means you have access to location services even when the app isn't in the foreground. However, you must have some active task such as navigation in process.
The Always API function isn't tantamount to constant background execution.
A task has to belong to a process. If the thread's process does not work, the thread can not work. So, the answer is the first, the background task runs only when started from the app. Because your background task belongs to your app that means your process.
You have to create a system thread if you want it to work even if your process kills. But, most operating systems do not allow the user to do if you do not develop a device driver.
Related
Hi I'm building an app that uses background location updates. The app uses region monitoring so it will be launched by the system into background automatically when user leaves a region that the app previously registered to monitor even when the app is not running (with 'always' location permission).
The question is, after my app finishes its task in the background and registers for a new region to monitor, it will keep running in the background (suppose that user does not use significant resources in other app that will cause my app to terminate to save memory and stuff). Since my app isn't really do anything now in the background, it will not consume a lot of power, but it will show up in the battery page with a very long background usage time which can be terrifying to the user.
Is there a way to programmatically terminate my app now to stop it from running in the background? (This is OK because the app has registered for a new region to monitor and will later be brought up again if user leaves)
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.
Is there any way in latest iOS to initiate location capture at a particular time each day (say morning and evening ), even if the app is in background or not running.
You should read Background execution. Background jobs are actually not recommended:
An app might move to the background because the user launched a different app or because the user locked the device and is not using it right now. In both situations, the user is signaling that your app does not need to be doing any meaningful work right now. Continuing to run in such conditions will only drain the device’s battery and might lead the user to force quit your app altogether.
As for getting user location check this answer How can I get current location from user in iOS
I'm little bit confused with background fetch. I read in Apple Developer documentation that fetch happens when OS decides that it should, user can't control background fetch, while on Apple Developer forum post by Apple employee says that if user kills app (double tap on home and button swipe up) background fetch wont happen, in that case user can control background fetch. So can someone please clarify to me if user kills the app with task manager will background fetch still continue in the background or it's killed at the same time as app.
Apple documentation:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW1
From the doc you link:
When a good opportunity arises, the system wakes or launches your app into the background and calls the app delegate’s application:performFetchWithCompletionHandler: method.
So, it seems that the system is able to launch in the background an app that is not running so it executes a background fetch. On the other hand, though, later in the document you can read:
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.
So, Apple's engineer is right: force quitting an app puts it into a sort of special case where background fetches are not allowed anymore.
If the user feels the need to allow background operations, he wouldn't kill the app. But when he kills it, it is only appropriate to disallow background fetch. User can only control if background fetch should happen or not by allowing it to stay in background/by killing the app. But once the app is in background, user cannot control "when" the background fetch happens. The OS determines it based on how free it is.
I think this quote (from the linked document) is the most important for the scenario you're describing:
Once configured, your NSURLSession object seamlessly hands off upload and download tasks to the system at appropriate times. If tasks finish while your app is still running (either in the foreground or the background), the session object notifies its delegate in the usual way. 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.
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.