Ali Shafiee
Ali Shafiee

Reputation: 13

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

Upvotes: 1

Views: 1775

Answers (2)

7vikram7
7vikram7

Reputation: 2824

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

Upvotes: 1

Ratul Sharker
Ratul Sharker

Reputation: 8011

Please have a look in apple doc.

From the guide there are some key points, you may interested in the following section from the apple documentation.

The significant-change location service is highly recommended for apps that do not need high-precision location data. With this service, location updates are generated only when the user’s location changes significantly; thus, it is ideal for social apps or apps that provide the user with noncritical, location-relevant information. If the app is suspended when an update occurs, the system wakes it up in the background to handle the update. If the app starts this service and is then terminated, the system relaunches the app automatically when a new location becomes available. This service is available in iOS 4 and later, and it is available only on devices that contain a cellular radio.

For implementation details take a look Getting location events background(ios)

Upvotes: 0

Related Questions