Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Location Updates for iOS App Even when Suspended

On early 2014, Apple has updated the iOS 7.0 to 7.1 in order to allow location updates even when the app is not active on foreground and not in the background. How do we do that?

I have read some articles like Apple's iOS 7.1 will fix a geolocation bug. But Apple didn't provide much communication related to that nor any sample code on how to get the location update even when the app is killed/terminated/suspended.

I have read the Release Notes for iOS 7.1. I couldn't find anything related to that as well.

So, how do we actually get the location update for iOS 7 and 8 even when the app is suspended?

like image 859
Ricky Avatar asked Jan 02 '15 13:01

Ricky


People also ask

How often does someone's iPhone location update?

In the new ‌Find My‌ app and with a good signal, the location beacons on the map in the People and Devices tabs are designed to refresh periodically and automatically every minute or so. If you think your connection's spotty or you don't want to wait for the next periodic refresh, you can perform a manual update.

How do you check if Location Services are enabled iOS?

Users can enable or disable location services by toggling the Location Services switch in Settings > Privacy.

What is background fetch in iOS?

Background fetch is a new mode that lets your app appear always up-to-date with the latest information while minimizing the impact on battery. You could download feeds within fixed time intervals with this capability. To get started: 1- Check Background Fetch in capabilities screen in Xcode.

Does Apple location update automatically?

Frequently Asked Questions about Find My iPhone From iOS 13, the “Find My” app automatically updates the shared location status after every minute. However, if you want, you can manually update the location status as well.


1 Answers

After months of trials and errors by experimenting the Core Location Framework, I have found the solution to get location update even when the app is killed/suspended. It works well for both iOS 7 and 8.

Here is the solution:-

If your app is a location based mobile application that needs to monitor the location of the device when it has significant changes, the iOS will return you some location coordinates when the device has moved more than 500 meters from the last known location. Yes, even when the app is killed/suspended either by the user or iOS itself, you still can get the location updates.

So in order for a locationManager to get location update even when the app is killed/suspended, you must use the method startMonitoringSignificantLocationChanges, you can not use startUpdatingLocation.

When iOS wants to return the location update to the app, it will help you to relaunch the app and return a key UIApplicationLaunchOptionsLocationKey to the app delegate method didFinishLaunchingWithOptions.

The key UIApplicationLaunchOptionsLocationKey is very important and you must know how to handle it. You must create a new locationManager instance when you receive the key and you will get the location update on the locationManager delegate method didUpdateLocations.

Here is the sample code:-

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{     self.shareModel = [LocationShareModel sharedModel];      if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {        self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];       self.shareModel.anotherLocationManager.delegate = self;       self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;       self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;        if(IS_OS_8_OR_LATER) {         [self.shareModel.anotherLocationManager requestAlwaysAuthorization];       }       [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];         }             return YES;  } 

In addition to the didFinishLaunchingWithOptions method, I have created the locationManager instance when the app is active. Here are some code examples:

- (void)applicationDidEnterBackground:(UIApplication *)application {     [self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];      if(IS_OS_8_OR_LATER) {         [self.shareModel.anotherLocationManager requestAlwaysAuthorization];     }      [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges]; }  - (void)applicationDidBecomeActive:(UIApplication *)application {     if(self.shareModel.anotherLocationManager)         [self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];      self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];     self.shareModel.anotherLocationManager.delegate = self;     self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;     self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;      if(IS_OS_8_OR_LATER) {         [self.shareModel.anotherLocationManager requestAlwaysAuthorization];     }      [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges]; } 

I have written an article explaining on the details on how to get the location update for iOS 7 and 8 even when the app is killed/suspended. I have also uploaded the complete source code on GitHub with the steps on how to test this solution.

Please visit the following URLs for more information:-

  1. Getting Location Updates for iOS 7 and 8 when the App is Killed/Terminated/Suspended
  2. Source Code on GitHub - Get the Location Updates Even when the iOS mobile apps is Suspended/Terminated/Killed
like image 132
Ricky Avatar answered Sep 20 '22 12:09

Ricky