Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active February 11, 2023 08:55
Show Gist options
  • Save lamprosg/5345446 to your computer and use it in GitHub Desktop.
Save lamprosg/5345446 to your computer and use it in GitHub Desktop.
(iOS) Running location updates in the background
-(void) applicationDidEnterBackground:(UIApplication *) application
{
// You will also want to check if the user would like background location
// tracking and check that you are on a device that supports this feature.
// Also you will want to see if location services are enabled at all.
// All this code is stripped back to the bare bones to show the structure
// of what is needed.
[locationManager startMonitoringSignificantLocationChanges];
/**********************************
You can use ordinary location services keep running if you set UIBackgroundModes to location.
(with no extra code if you initialize it in the app delegate didFinishLaunchingWithOptions)
You can do this in the YourApp-Info.plist file:
Add a new line and put "Required background modes".
Then open the table and add an item with the value "App registers for location updates".
This will let you use standard location services (as opposed to the significant change location service)
http://mobile.tutsplus.com/tutorials/iphone/ios-multitasking-background-location/
**********************************/
}
-(void) applicationDidBecomeActive:(UIApplication *) application
{
//Go to higher accuracy
[locationManager stopMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
}
#pragma mark - Location manager delegate
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
isInBackground = YES;
}
// Handle location updates as normal, code omitted for brevity.
// The omitted code should determine whether to reject the location update for being too
// old, too close to the previous one, too inaccurate and so forth according to your own
// application design.
if (isInBackground)
{
[self sendBackgroundLocationToServer:newLocation];
}
else
{
// ...
}
}
-(void) sendBackgroundLocationToServer:(CLLocation *)location
{
// REMEMBER. We are running in the background if this is being executed.
// We can't assume normal network access.
// bgTask is defined as an instance variable of type UIBackgroundTaskIdentifier
// Note that the expiration handler block simply ends the task. It is important that we always
// end tasks that we have started.
//TELL the operating system in advance that we are doing a background task that should be allowed to run to completion
//Check if our iOS version supports multitasking
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
{
//Check if device supports mulitasking
if ([[UIDevice currentDevice] isMultitaskingSupported])
{
UIBackgroundTaskIdentifier bgTask;
bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:
^{
[[UIApplication sharedApplication} endBackgroundTask:bgTask];
}];
// ANY CODE WE PUT HERE IS OUR BACKGROUND TASK
// For example, I can do a series of SYNCHRONOUS network methods (we're in the background, there is
// no UI to block so synchronous is the correct approach here).
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
locationManager.distanceFilter = 100;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
// Synchronize the cleanup call on the main thread in case
// the expiration handler is fired at the same time.
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
});
}
}
}
/*************************************************************/
//Check this out
https://gist.github.com/lamprosg/5046828
/*************************************************************/
@lamprosg
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment