J'essaie d'activer le mode de localisation en arrière-plan dans mon application. J'ai activé le mode d'arrière-plan 'Location updates' dans mon fichier plist. L'application contient une minuterie qui est mise à jour toutes les 15 secondes.Emplacement Le mode Arrière-plan ne fonctionne pas sur iOS
Lorsque l'application navigue à fond, je fais ce qui suit
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication* app = [UIApplication sharedApplication];
self.bgTaskID = [app beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"background task %lu expired", (unsigned long)self.bgTaskID);
[app endBackgroundTask:self.bgTaskID];
self.bgTaskID = UIBackgroundTaskInvalid;
}];
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(initializeLocationManager) userInfo:nil repeats:NO];
if(self.timerLocationBackground)
{
[self.timerLocationBackground invalidate];
self.timerLocationBackground = nil;
}
self.timerLocationBackground = [NSTimer scheduledTimerWithTimeInterval:15
target:self
selector:@selector(initializeLocationManager)
userInfo:nil
repeats:YES];}`
Le initializeLocationManager est inférieure à
-(void)initializeLocationManager
{
if(!self.locationManager)
self.locationManager = [[CLLocationManager alloc] init];
else
[self.locationManager stopUpdatingLocation];
if ((![CLLocationManager locationServicesEnabled])
|| ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted)
|| ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied))
{
//user has disabled his location
}
else
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
[self.locationManager startUpdatingLocation];
}
}
Quand je navigue à l'application après 10 minutes pour ex, mon minuteur est arrêté à 3 min, ce qui est le moment de la suspension de l'application.
Mon code lorsque l'application de revenir à l'avant-plan est le suivant:
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
//
//Remove the baground task
//
if (self.bgTaskID != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:self.bgTaskID];
self.bgTaskID = UIBackgroundTaskInvalid;
}
[self.locationManager stopUpdatingLocation];
Toute aide?
Pouvez-vous poster le code où vous définissez self.bgTaskID? – rjpadula
@rjpadula Mise à jour, veuillez cocher la première ligne de la méthode didenterbackground –
Etes-vous en mesure de voir si votre message "tâche de fond% lu expiré" est affiché? Il est probable que vous deviez renouveler votre tâche d'arrière-plan. D'après mon expérience, trois minutes correspondent au temps que vous avez jusqu'à ce que vous en ayez besoin. – rjpadula