Search code examples
iosobjective-ciphonebackgroundios9

How to get location periodically and send location to server in background ios9


I want my app to get the location every 15 minutes and send the location information to a server. In the server, I compared locations and send a response to the client as a notification (almost like a push notification but on my server).


Solution

  • You can set the CLLocation Managers authorization to requestAlwaysAuthorization to fetch location continuously (even when your app is in the background.)

    self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
        {
            [self.locationManager requestAlwaysAuthorization];
        }
    

    Note: requestAlwaysAuthorization is battery intensive and apple reviewers will expect a very strong reason for using this for your app to be approved. Also, make sure that “location update” under the required Background Modes is checked.

    You can use a NSTimer to determine 15 mins and get location from the above CLLocationManager.

    After that, you can use a background task to update location to the server.

    You can follow the below links if you have any doubts: http://www.creativeworkline.com/2014/12/core-location-manager-ios-8-fetching-location-background/ http://mobileoop.com/background-location-update-programming-for-ios-7 Periodic iOS background location updates