Ali Shafiee Ali Shafiee - 8 months ago 57
Objective-C Question

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).


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: Periodic iOS background location updates