Why my location service is not running in background - ios

My core location code is working fine all delegates are working properly, I can view exact location in mapview. Now I want this application to keep running in background, I searched on internet and found only few services are allowed to work in background and other suspend in few minutes. For enabling thos services I set a flag of uibackgroundmode = location and my delegate on core location is also working but still why my application suspends after running 20 minutes?

There are a few reasons why this doesn't work. First try the answer of this question:
Specify location background mode
Use an NSTimer in the background by using UIApplication:beginBackgroundTaskWithExpirationHandler:
In case n is smaller than UIApplication:backgroundTimeRemaining it does works just fine, in case n is larger, the location manager should
be enabled (and disabled) again before there is no time remaining to
avoid the background task being killed. This does work since location
is one of the three allowed types of background execution.

Related

Significant-Change Location Service vs. Standard Location Service in Swift

I have an app which supports background monitoring. For my app to be able to register background movement of device I must call startMonitoringSignificantLocationChanges(). However, at some point in my code I must also call startUpdatingLocation().
My question is, if I call startMonitoringSignificantLocationChanges() and later in my code I call startUpdatingLocation(), will significant monitoring be overridden? Specifically, if I terminate my app will my location manager still receive updates on significant location changes?
After a few testings I’ve come to a conclusion that even if “live” location updates are activated, significant location changes notifications will still be received while running app in background or when terminated. Hope others will find this useful.

iOS background location tracking after reboot

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.

can we get location updates in IOS swift in a regular intervel of time even when app is not running in foreground

I want to get location updates of the user in a regular interval of time even if the app is not running in foreground.
In most of the articles i have read, they said that the OS will forcefully stop or suspend the background service the app have started.
What i need is the app should regularly check the user location and when that location becomes greater than say 10Kms the app should trigger a local notification.
We were able to do the functionality correctly when the app is ran again by the user. But it wont work in background. And if we inegrated it as a background service , then it is causing the app to crash. :(
this part caught my attention after a long time of search
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW8
As far as I understood, they say that the OS will wakeup the app to get the current location when it senses a change in location.
But I didnt get any helping tutorial to accomplish the same
These are the tutorials which I refered
Periodic iOS background location updates
How do I get a background location update every n minutes in my iOS application?
Getting user location every n minutes after app goes to background
iOS Not the typical background location tracking timer issue
iOS long-running background timer with "location" background mode
What i need is preferably a background service or something which does the job done which check of the current location calculate distance and makes a local notification.
But as far as i know all the background services will be suspended or killed by the OS within some time after the app is gone in background.
Please guys I am desperate, Its been 2 weeks i am on in its tail.
Building the same for Android was a piece of cake actually.
Any help ???
You can get background location updates easily, you need to enable "Location updates" under "Background Modes" section in capabilities.
You also need to request for Always Authorisation and finally add this
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
}
Make sure "Location updates" is activated else the above code will lead to a crash.
Also, write the CLLocationManager Delegate Methods in your AppDelegate class as this would increases chances of those methods being called as mentioned on Raywenderlich Background Location tutorial

iOS Location - Significant Location Change in iOS 8/9 can start standard location service?

Many questions concerning iOS Location tracking have been asked on here, but recently Apple has updated much of their functionality and so many of the answers are obsolete, my question is specifically targeting iOS 8 and 9.
I am interested in creating a location tracking application which will accurately track the user even when the app is closed. To do this, apple states:
If you leave the significant-change location service running and your
iOS app is subsequently suspended or terminated, the service
automatically wakes up your app when new location data arrives. At
wake-up time, the app is put into the background and you are given a
small amount of time (around 10 seconds) to manually restart location
services and process the location data.
So my understanding is, if you have all of the necesarry permission and plist keys present, the OS will wake up your app and allow you to do something for 10 seconds, or if it needs more time it can:
If an iOS app needs more time to process the location data, it can
request more background execution time using the
beginBackgroundTaskWithName:expirationHandler: method of the
UIApplication class.
Now, this says that if my app needs more time to process the location data, it may start a background task (with an expirationHandler) to handle that. When reading about background tasks, I read that background tasks can run for a maximum of 10 minutes
So my overall question is, when my app gets woken up when a significant location change has occurred, is it possible to start the standard location service and have it run in the background indefinitely? Is it possible to start another background task before the first one expires?
EDIT:
From the Apple Documentation it says:
because it wakes the system and your app at least every 15 minutes,
even if no location changes have occurred, and it runs continuously
until you stop it
So my idea is, if the user starts their phone from nothing and never opens the app, after at most 15 minutes (even if they are standing still), the OS will fire my app, in which case I could start a background process that I can start the GPS tracking in, and thus getting accurate GPS location. Would this logic work with the OS?
The main problem is that significant location changes cannot be used for
"accurately track" the user.
Significant means 1000m, since it is cell tower based, and not GPS based.
So your app is woke up when the user moves into the next GSM cell.
This is not acurate enough for acurate tracking.
finally this means you cannot acuratley track when the app is closed, this works only in running app or background running up.
"When reading about background tasks, I read that background tasks can
run for a maximum of 10 minutes"
No, thats not true. My app runs in background and records GPS locations until the battery is drained (8h+). (It does not use the significant location mode)

Core Data misses to save context with region monitoring when app is backgrounded

I have a rather strange behaviour in an app that is monitoring some CLCircularRegion objects.
What the app is expected to do
Whenever a region is entered or left, an entry in a quite simple UIManagedDocument (not located in the iCloud) will be updated or created. Furthermore, a NSLocalNotification will be fired telling me which region has been affected and how long the user stayd within the region.
What really happens
The Notifications are fired as expected, so the delegate methods are fired. Even the duration is calculated correctly. BUT, whenever the app was in background for a longer time - what means about an hour or more - there are no objects updated or created in the UIManagedDocument.
This behaviour is hard to debug, because I cannot reproduce it with the debugger attached or in Simulator, just on my iPhone 5S running iOS 8.1.3.
What I already tried out
Even if the UIManagedDocument handles context saving by itself, I tried to call the context saving directly from the location managers delegate method. Without any success.
Turning on the location update background capability lead the app to be rejected from the app store, because there is no need to do background location updates when working with region monitoring.
Question
Does anyone out there face similar problems or knows how to do the trick?
thank you in advance,
Peter
Are you aware that the iOS app gets awoken just for a couple of seconds after entering a region in background ? After that it stops execution and gets suspended.
Maybe you should try putting your UIManagedDocument operation in a backgroundTask. Please read Executing Finite-Length Tasks in:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Resources