Erweitern Sie das Limit des Hintergrundprozesses in iOS 10.3.2 und höheren VersionenIOS

Programmierung für iOS
Guest
 Erweitern Sie das Limit des Hintergrundprozesses in iOS 10.3.2 und höheren Versionen

Post by Guest »

Ich verwende eine Anwendung, die alle 15 Minuten den Standort des Geräts an den Server sendet. Die Logik befindet sich in didUpdateToLocation. Ich plane einen Timer, um die Methode restartLocationUpdates alle
180 Sekunden (3 Minuten) aufzurufen und die Standortaktualisierungen durch Aufruf von stopLocationDelayBy10Seconds< zu stoppen /strong> nach 10 Sekunden, um den Akku zu schonen.

Code: Select all

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
//some business logic goes here....
if (sendLocation) {
//send location to server
}

UIApplication* application = [UIApplication sharedApplication];

__block UIBackgroundTaskIdentifier bgTaskId = UIBackgroundTaskInvalid;
if ([application respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) {
bgTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTaskId];
bgTaskId = UIBackgroundTaskInvalid;

}];
//using a timer to schedule a method call
self.bgtimer = [NSTimer scheduledTimerWithTimeInterval:180 target:self
selector:@selector(restartLocationUpdates)
userInfo:nil
repeats:NO];

//stopping the locatioupdates after 10 seconds to save battery
self.delayTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self
selector:@selector(stopLocationDelayBy10Seconds)
userInfo:nil
repeats:NO];
}

- (void) restartLocationUpdates {
counter++;
if(counter == 5){
sendLocation = YES;
counter=0;
}else{
sendLocation = NO;
}
//invalidating the bgtimer before creating and scheduling it again.
if (self.bgtimer) {
[self.bgtimer invalidate];
self.bgtimer = nil;
}
//configuring location manager and then starting location updates
//.....
[locationManager startUpdatingLocation];
}

-(void)stopLocationDelayBy10Seconds{
[locationManager stopUpdatingLocation];
}
Vor ios10 funktionierte es einwandfrei, aber nach iOS10.3.2 funktioniert der BGTimer nicht mehr konsistent, er hört nach 10 Minuten auf zu funktionieren, da die App in den Hintergrundzustand wechselt. Kann mir jemand helfen, was mit dem Code nicht stimmt und wie ich den Timer weiterlaufen lassen kann, während die App im Hintergrund läuft?
Imp: Die Berechtigung für Ortungsdienste ist auf „Immer“ eingestellt und der Hintergrundmodus ist aktiviert Standortaktualisierungen und Hintergrundabruf

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post