【问题标题】:Location Background Mode is not working on iOS位置背景模式在 iOS 上不起作用
【发布时间】:2016-10-26 17:25:51
【问题描述】:

我正在尝试在我的应用程序中启用后台定位模式。我在我的 plist 文件中启用了“位置更新”后台模式。 该应用程序包含一个每 15 秒更新一次的计时器。

当应用程序导航到后台时,我正在执行以下操作

- (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];}`

initializeLocationManager 在下面

  -(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];
    }
}

当我在 10 分钟后导航回应用程序时,我的计时器在 3 分钟停止,这是应用程序暂停的时间。

当应用返回前台时我的代码如下:

- (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];

有什么帮助吗?

【问题讨论】:

  • 你能把设置 self.bgTaskID 的代码贴出来吗?
  • @rjpadula 更新了,请检查,是在 didenterbackground 方法中设置的第一行
  • 您能否看到“后台任务 %lu 已过期”消息是否正在显示?您可能需要更新后台任务。根据我的经验,三分钟是你需要多长时间。
  • @rjpadula 我该如何续订?位置检索不应该让应用保持清醒吗?

标签: ios cllocationmanager xcode8 uibackgroundtask background-mode


【解决方案1】:

应该是这样的。我自己已经切换到 swift,所以我的 Objective-c 可能并不完美。

- (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] 
    }];
}

【讨论】:

  • 我试图删除旧的后台任务,如果有的话(我认为这是你唯一的改变),但是当我回到 3 分钟后我仍然暂停了计时器前景。我对这个问题感到绝望!
  • 我所做的更改使得到期处理程序将在第一个到期时(即您看到的 3 分钟)创建一个新的后台任务。抱歉发错了。
  • 我试过上面的代码还是不行,我猜是你后台任务完成后调用keepAwakeInBackground方法的时候,self.bgTaskID = UIBackgroundTaskInvalid;正在执行,则应用会自动设置为挂起状态,而不继续执行新任务。
  • 我获取的位置更新和设置启用的后台模式位置应该不足以让应用程序保持活力??我不明白出了什么问题
  • keepAwakeInBackground 应该在“时间到期之前”根据Apple's Docs 被调用然后该方法使旧任务到期并开始一个新任务,这应该使应用程序保持活动状态(并且正在为我工​​作) .我不明白为什么这对你不起作用。我从this thread得到了很多帮助
猜你喜欢
  • 2011-08-27
  • 2011-07-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 2012-11-29
  • 2016-07-29
  • 2021-03-11
相关资源
最近更新 更多