0

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?

+0

Pouvez-vous poster le code où vous définissez self.bgTaskID? – rjpadula

+0

@rjpadula Mise à jour, veuillez cocher la première ligne de la méthode didenterbackground –

+0

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

Répondre

0

Doit être quelque chose comme ceci. Je suis passé à la vitesse supérieure, donc mon objectif-c pourrait ne pas être parfait.

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

    [self keepAwakeInBackground ] ; 

    /// rest of your function to set up timers 
} 

- (void) keepAwakeInBackground { 

    //Remove the old background task, if there was one 
    if (self.bgTaskID != UIBackgroundTaskInvalid) { 
     [[UIApplication sharedApplication] endBackgroundTask:self.bgTaskID]; 
     self.bgTaskID = UIBackgroundTaskInvalid; 
    } 
    /* 
    * You probably want something here to decide that the app should really be suspended here 
    * if () return 
    */ 

    // Set up new background task 
    UIApplication* app = [UIApplication sharedApplication]; 
    self.bgTaskID = [app beginBackgroundTaskWithExpirationHandler:^{ 
     [self keepAwakeInBackground] 
    }]; 
} 
+0

J'ai essayé de supprimer l'ancienne tâche d'arrière-plan, s'il y en avait une (je pense que c'est le seul changement de votre part), mais j'ai quand même arrêté la minuterie après 3 minutes quand je retourne au premier plan. Je suis désespérée à propos de ce problème! –

+0

La modification que j'ai faite fait en sorte que le gestionnaire d'expiration va créer une nouvelle tâche d'arrière-plan lorsque le premier expire (ce qui est les 3 minutes que vous voyez). Désolé de l'avoir mal posté. – rjpadula

+0

J'ai essayé le code ci-dessus mais ne fonctionnait toujours pas, je suppose que lorsque vous appelez la méthode keepAwakeInBackground lorsque la tâche d'arrière-plan est terminée, et self.bgTaskID = UIBackgroundTaskInvalid; est en cours d'exécution, puis l'application est définie automatiquement à l'état suspendu sans continuer à la nouvelle tâche. –