CLLocationManager startMonitoringForRegion: not relaunching app after device restart - ios

Ok, so I currently have an app where I register a geofence to be monitored using the CLLocationManager startMonitoringForRegion method. This works fine when the app is in the foreground and background.
I also have the appropriate plist values set:
UIBackgroundModes :{location}
UIRequiredDeviceCapabilities: {location-services}
What doesn't work
After a device restart, the app is not being relaunched. I can force this to happen if I set startMonitoringSignificantLocationChanges before entering background. But this method uses much more battery life and I don't need the location all the time, just whenever I break a geofence.
The docs for regions say:
In iOS, the regions you register with the location manager persist between launches of your application. If a region crossing occurs while your iOS app is not running, the system automatically wakes it up (or relaunches it) in the background so that it can process the event. When relaunched, all of the regions you configured previously are made available in the monitoredRegions property of any location manager objects you create.
Question
Is it possible (when using geofencing regions) to have the system restart my app AFTER a system reboot, WITHOUT using startMonitoringSignificantLocationChanges ?
Edit: I am targeting iOS6 & iOS7

Answering my own question here.
While you do need to use the startMonitoringSignificantLocationChanges to get the system to wake up the app after a device reboot. Nothing will wake up the app if the user has force closed it.
For my purposes, I did not need to set UIBackgroundModes to location. This setting is usually used for applications that only need fine-grained location updates (E.g. using startUpdatingLocation). Setting the background mode is not required when using startMonitoringSignificantLocationChanges.

Related

iOS: Location Update in Application Suspended State

Is it possible to update location when application is in suspended/terminate state.
i want to update location every 500 meters when app is in suspended/terminate state.
thanks in advance.
When terminated the only real way to get updates is to use Region Monitoring but you will only get major updates so I doubt it will work for 500 meters.
From the documentation:
If your app is terminated either by a user or by the system, the
system doesn't automatically restart your app when new location
updates arrive. A user must explicitly relaunch your app before the
delivery of location updates resumes. The only way to have your app
relaunched automatically is to use region monitoring or
significant-change location service. However, when a user disables the
Background App Refresh setting either globally or specifically for
your app, the system doesn't relaunch your app for any location
events, including significant change or region monitoring events.
Further, while Background App Refresh is off your app won't receive
significant change or region monitoring events even when it's in the
foreground.
There are good reasons for this. Firstly people don't want apps that 'snoop' on them all the time and even if there is a good reason imagine having 10 apps doing this constantly. It's going to start causing you performance and battery life issues.
EDIT
There appears to be some confusion by a lot of people over this issue so I will attempt to clear things up a bit.
Your app is NOT supposed to be continually tracking location while terminated (or suspended really). You are just not allowed to do this both for privacy reasons and so that you don't drain the battery excessively.
While your app is in the foreground you can use location services via startUpdatingLocation to monitor the device location. Assuming the app has been granted permission this will use all available hardware (GPS, WiFi, Cellular).
Now from the documentation itself:
If you start this service and your app is suspended, the system stops
the delivery of events until your app starts running again (either in
the foreground or background). If your app is terminated, the delivery
of new location events stops altogether. Therefore, if your app needs
to receive location events while in the background, it must include
the UIBackgroundModes key (with the location value) in its Info.plist
file.
So once suspended and terminated your options are really to monitor regions and to monitor for significant location changes. Neither of these are particularly accurate or frequent. One reason for this is that they only use low power methods to get the position (WiFi and Cellular) they don't use the GPS.
So no accurate and/or frequent location tracking while an app is suspended or terminated. This just has to be accepted and you need to design your apps accordingly.
let locationManager = CLLocationManager()
locationManager.startMonitoringSignificantLocationChanges()
After returning a current location fix, the receiver generates update events only when a significant change in the user’s location is detected. It does not rely on the value in the distanceFilter property to generate events
If you start this service and your app is subsequently terminated, the system automatically relaunches the app into the background if a new event arrives. In such a case, the options dictionary passed to the application(:willFinishLaunchingWithOptions:) and application(:didFinishLaunchingWithOptions:) methods of your app delegate contains the key location to indicate that your app was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.
Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.
Hope It helps

IOS Getting location updates when app terminated without using significantChange

I apologise for the redundancy of this topic, but in spite all the given answers, I can't identify the possibility of getting accuracyBest location updates when the app is terminated.
I don't want to use monitoringSignificantChange, I want the best possible accuracy; I won't submit the app on the AppStore, so Apple restrictions are not a problem either.
I have gone through these:
-Location update even when app is killed/terminated
-iOS update location even when app is terminated
-Working with location updates when app is terminated
-http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended
and many more, but it is not clear whereas it's possible or not. I currently have my project, everything works great with significantChange but I now need better accuracy.
Could somebody tell me straight forward if getting best accuracy location update when app is killed is possible please ?
Thank you infinitely,
Looking at the below content from the Apple doc, you clearly have 2 alternatives against using significant location changes to wake a app from the background. I have marked in bold the services you can use to relaunch the app if it has been terminated.
Using Location Services in the Background Most location services are
meant to be used while your app is in the foreground but some can also
run in the background. In some cases, location events can even cause
your app to be relaunched to process an event. To run most location
services in the background, you need to enable the location updates
background mode in the Capabilities tab of your Xcode project. For
services that launch your app, you need to request (and be granted)
“Always” authorization from the user.
The standard location service delivers events normally while an app is
running in the foreground. When your app is in the background, this
service delivers events only when the location-updates background mode
is enabled for the app. This service does not relaunch iOS apps that
have been terminated.
The significant location change service delivers events normally
while an app is running in the foreground or background. For a
terminated iOS app, this service relaunches the app to deliver
events. Use of this service requires “Always” authorization from the
user.
The region monitoring service delivers events normally while an
app is running in the foreground or background. (You can use this
service for both geographic and beacon regions.) For a terminated
iOS app, this service relaunches the app to deliver events. Use of
this service requires “Always” authorization from the user.
Beacon ranging delivers events normally while an app is running in the
foreground. When your app is in the background, this service delivers
events only when the location-updates background mode is enabled for
the app and the standard location service is running. (If the beacon
region’s notifyEntryStateOnDisplay property is YES, waking the device
causes the app to range for beacons for a few seconds in the
background.) This service does not relaunch iOS apps that have been
terminated; however, you can be relaunched by monitoring beacon
regions using the region monitoring service.
The heading service delivers events normally while an iOS app is
running in the foreground. When your app is in the background, this
service delivers events only when the location-updates background mode
is enabled for the app and the standard location service is running.
This service does not relaunch iOS apps that have been terminated.
The visit service delivers events normally while an iOS app is
running in the foreground. When your app is in the background, this
service delivers events only when the location-updates background mode
is enabled for the app and the standard location service is running.
For a terminated iOS app, this service relaunches the app to deliver events. Use of this service requires “Always” authorization from the
user.
Enabling the location-updates background mode ensures that an app
continues to receive location events while in the background. When the
app moves to the background, the system adds the location-services
indicator to the status bar to let the user know that an app is using
location services. The system may still terminate the app at any time
to reclaim its memory or other resources.
Also from the doc,
Getting the Visited Locations
In iOS, the visits service provides an alternative to the significant location change service for apps that need location
information about interesting places that the user visited. For
example, if the user is in one location for an extended period of
time, the service might generate an event when the user arrives at
that location and another when the user leaves that location. The
service is intended for apps that might already be using the
significant location change service and want an even lower power way
to do so. You would not use this service to create navigation apps or
apps that rely on regular location updates.
Document Link:
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/doc/uid/TP40007125-CH3-SW73
Continuous location updates stop when your app is terminated.
Geo-fence notices will re-launch your app even if it's not running. I think a significant location change will also relaunch you're app but I'm not as sure about that.
Once you're relaunched you can start location updates again with the accuracy set to best like you want, but I think you're going to need to use one of those two APIs to get relaunched again after you've been terminated.

Does location update remain running after force close app

I am sending user location at n time on server for tracking. Working fine in foreground and background mode. But my question is bit more theoretical.. Does my location update service remain running after i force close the app on the device?
I know i can check it on server, But sorry i don't have server access permission. So i have to be sure in which scenario my location service will stop running.
Yep it will continue to be updated when the application is sent into the background. For some official docs check here under the "Getting Location Events in the Background (iOS Only)" section.
Also, under special circumstances you can register your application to have a true background process running like Android allows. So when the user kills the background application, your app doesn't really die. The background process continues to work. Apple will only allow apps that have a necessary reason for this though. Such as a turn by turn navigation app (example from the docs).
Yes, if the application is removed from background then location update will be stopped.
iOS will only restart an app after a force close if its uses region monitoring or the significant-change location service. From the iOS Location and Maps Programming Guide section on Getting the User’s Location:
If your app is terminated either by a user or by the system, the system doesn’t automatically restart your app when new location updates arrive. A user must explicitly relaunch your app before the delivery of location updates resumes. The only way to have your app relaunched automatically is to use region monitoring or the significant-change location service.
iOS only restarts the app if it has Background App Refresh enabled:
when a user disables the Background App Refresh setting either globally or specifically for your app, the system doesn’t relaunch your app for any location events, including significant-change or region monitoring events.

Is there any way to wake my application from killed state(killed from app switcher)?

I am creating an application that sends updated location to server regardless of app state. I have tried silent push to wake my application in background but it didnt work (it works fine except if my app is killed by user from app switcher). I am using location manager for getting current location.
Does IOS7 provides this feature to launch your application in background (even if app is killed by user from app switcher)??
I have spent five days without any success on this. If anyone can help me that would be highly appreciable.
Thanks in advance!!
If you need precise GPS locations you can set add the "location" value to the UIBackgroundModes array in your Info.plist as described in "UIBackgroundModes location and significant location changes with region monitoring". If you need only approximate locations you can use the startMonitoringSignificantLocationChanges method of CLLocationManager. As it is written in the documentation: "If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrives."
You can wake your app by using significantlocationchange.
You have to start significant location change while you switch your app to background.
Then system will wake your app after significant location change.
The frequency is not faster than 5 min and for every 500 meters.
You will have to catch the event by checking for LocationKey in didFinishLaunchingWithOptions in projects AppDelegate.

If background applications can't launch automatically how does Cardcase launch on a location change?

The Cardcase application lets you know if you approach a shop which you have a previous relationship with (if you've set up a tab for payments there etc.).
If background running applications cannot be started automatically on device boot, how does this app therefore work?
Edited to make my question clearer: I am not asking how to monitor for location changes or how to monitor for location changes in the background. My question is, if the user install this app, then adds some tabs for some locations, then they reboot their iPhone, then how can this app subsequently notify them when they approach a location if application launch on boot is not supported?
Must this application (and indeed any other application) be dependent upon it being launched explicitly by the user following every device reboot?
You can do this with region monitoring. You register regions that you would like to monitor with this method on CLLocationManager:
- (void)startMonitoringForRegion:(CLRegion *)region desiredAccuracy:(CLLocationAccuracy)accuracy
Then even if your app is not running, it will be launched in the background as the user enters or leaves the region.
From Apple's docs:
If you begin monitoring a region and your application is subsequently
terminated, the system automatically relaunches it into the background
if the region boundary is crossed. In such a case, the options
dictionary passed to the application:didFinishLaunchingWithOptions:
method of your application delegate contains the key
UIApplicationLaunchOptionsLocationKey to indicate that your
application was launched because of a location-related event. In
addition, creating a new location manager and assigning a delegate
results in the delivery of the corresponding region messages.
EDIT
Just tested this with my own app. Rebooted my phone before leaving the office, and the app was launched on my drive home as usual. So yes, this definitely survives a reboot.
Hope that helps.
This works for geo-fencing apps (apps that use startMonitoringForRegion).
My guess is that when you install apps using this class, the responsible iOS framework automatically registers an launchd script for the app background service startup inside iOS.
Of course this is all done automatically as you don't have fine control on iOS launch services unless you're jailbroken.
You can use the startMonitoringForRegion on your app, jailbreak your device and check on launchd to check how this works under the hood.

Resources