This blog includes updation of location while your app is in background mode. Sometimes we need to fetch the user location while the app is in a background. To achieve this first we need to use the location manager.
 
Here is the code for run the background task.write this in your AppDelegate.m class.
//run background task
-(void)runBackgroundTask: (int) time{
    
    //func use to get the current location
    [self getCurrentLocation];
    
    //check if application is in background modeg
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
        
        //create UIBackgroundTaskIdentifier and create tackground task, which starts after time
        __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            //schedule the timer
            NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(updateLocation) userInfo:nil repeats:NO];
            [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
            [[NSRunLoop currentRunLoop] run];
        });
    }else if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(updateLocation) userInfo:nil repeats:NO];
            [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
            [[NSRunLoop currentRunLoop] run];
        });
    }
}
 
Here is the code to get current location-
//declare global location manager
CLLocationManager *locationManager;
UIApplication *app;
 
Initialize location manager viewdidload()-
//create CLLocationManager variable
    locationManager = [[CLLocationManager alloc] init];
    
    if ([Utils isIos8]) {
        if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [locationManager requestAlwaysAuthorization];
        }
        [locationManager requestAlwaysAuthorization];
    }
    
    //set delegate
    locationManager.delegate = self;
    
    app = [UIApplication sharedApplication];
 
-(void)getCurrentLocation
{
        [locationManager startUpdatingLocation];
        [locationManager setAllowsBackgroundLocationUpdates:YES];
}
 
Use the following code in appdelegate class-
//starts when application switchs into background
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    //check if application status is in background
    if ( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
        NSLog(@"start backgroun tracking from appdelegate");
        //start updating location with location manager
    }
    
    //change locationManager status after time.time is in seconds.
    //here it will update location after 5 minutes.
    [self runBackgroundTask:60*5];
    
}
 
                       
                    
0 Comment(s)