How do I get a background location update every n minutes in my iOS application?

后端 未结 14 1462
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 02:00

I\'m looking for a way to get a background location update every n minutes in my iOS application. I\'m using iOS 4.3 and the solution should work for non-jailbroken iPhones

14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 02:11

    To someone else having nightmare figure out this one. I have a simple solution.

    1. look this example from raywenderlich.com-> have sample code, this works perfectly, but unfortunately no timer during background location. this will run indefinitely.
    2. Add timer by using :

      -(void)applicationDidEnterBackground {
      [self.locationManager stopUpdatingLocation];
      
      UIApplication*    app = [UIApplication sharedApplication];
      
      bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
          [app endBackgroundTask:bgTask];
          bgTask = UIBackgroundTaskInvalid;
      }];
      
       self.timer = [NSTimer scheduledTimerWithTimeInterval:intervalBackgroundUpdate
                                                    target:self.locationManager
                                                  selector:@selector(startUpdatingLocation)
                                                  userInfo:nil
                                                   repeats:YES];
      
      }
      
    3. Just don't forget to add "App registers for location updates" in info.plist.

提交回复
热议问题