Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • User's location update on server when app is in background.

    • 0
    • 3
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 221
    Comment on it

    Code for those Apps where we need to update user's current location in regular interval of time even if app is in background state.

    In Appdelegate.h write this code:-

    @property (nonatomic,strong) UIBackgroundTaskIdentifier *bgTask;

    In AppDelegate.m write this code:-

    -(void) applicationDidEnterBackground:(UIApplication *) application
    {
        // First 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.
    
           [locationManager startMonitoringSignificantLocationChanges];
    }
    -(void) applicationDidBecomeActive:(UIApplication *) application
    {
           [locationManager stopMonitoringSignificantLocationChanges];
           [locationManager startUpdatingLocation];
    }
    -(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.
        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.
    
        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).
    
        // ...
    
        // AFTER ALL THE UPDATES, close the task
    
        if (bgTask != UIBackgroundTaskInvalid)
        {
            [[UIApplication sharedApplication} endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }
    }

    Happy Coding :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: