简体   繁体   中英

After 15 mins Background Location update is not working in ios?

I have implemented background location update in ios based on the timer (like every 1 min). Its working good and getting location(lat and Long values) but after 15 it is disconnecting. I'm not able to trigger it anymore. Can any one please help me out.

This is my code..

- (void)applicationDidEnterBackground:(UIApplication *)application {

    [[GlobalClass sharedGlobals].locationManager stopMonitoringSignificantLocationChanges];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
        [[GlobalClass sharedGlobals].locationManager requestAlwaysAuthorization];
    }
    [[GlobalClass sharedGlobals].locationManager startMonitoringSignificantLocationChanges];

    int timeInterval = 60; // (i.e., 1 mins )

    if (!setTimer) {
        setTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target: self
                                                  selector: @selector(toMakeServerCall) userInfo: nil repeats: YES];
    }
}

#pragma mark - Send Current lat & long to the server

- (void)toMakeServerCall {

    NSString *latitude = [NSString stringWithFormat:@"%f",[[GlobalClass sharedGlobals] currentLocationObject].coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f",[[GlobalClass sharedGlobals] currentLocationObject].coordinate.longitude];

    NSLog(@"Current Lat is :%@ \n Current Long is :%@ ",latitude,longitude);

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:www.google.com/myLatitude is: %@ and My Longitude is: %@",latitude,longitude]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

}

Thanks in advance.

When you use "significant change" service (or frankly any location services), you don't use a timer. The OS calls you when there's a change in location, not the other way around.

Regarding your timer, when the app is suspended, the timer won't run any more. So, don't use a timer. But with significant change service, when the device detects that it has been moved a significant distance, your app will we awaken and be informed. As the Location and Maps Programming Guide: Starting the Significant-Change Location Service says:

If you leave the significant-change location service running and your iOS app is subsequently suspended or terminated, the service automatically wakes up your app when new location data arrives. At wake-up time, the app is put into the background and you are given a small amount of time (around 10 seconds) to manually restart location services and process the location data. (You must manually restart location services in the background before any pending location updates can be delivered, as described in Knowing When to Start Location Services .) Because your app is in the background, it must do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. If it does not, your app will be terminated. If an iOS app needs more time to process the location data, it can request more background execution time using the beginBackgroundTaskWithName:expirationHandler: method of the UIApplication class.

Location updates may be paused even if you set pausesLocationUpdatesAutomatically to false. The good thing is that you can get notified when the updates are paused, so you can restart location updates.

Define locationManagerDidPauseLocationUpdates in your CLLocationManagerDelegate like this:

func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) {
    manager.startUpdatingLocation()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM