gpt4 book ai didi

iphone - 在 iOS 中暂停时启动 locationManager

转载 作者:行者123 更新时间:2023-12-01 16:55:16 25 4
gpt4 key购买 nike

我搜索了很多天的解决方案,但我没有找到任何让我满意的解决方案。

我已启动LocationManager ,并且工作正常。现在,我想连接 LocationManager有时间表。例如,在上午 9 点到下午 1 点之间,位置管理器应该工作,在下午 1 点到下午 3 点之后应该关闭,然后到午夜位置管理器必须打开。

当应用程序暂停和 LocationManager 时如何触发操作已关闭?

我写了这样的东西:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(doInBackground:) userInfo:nil repeats:YES];

- (void) doInBackground:(NSTimer*)time {
ScheduleModel *schMod = [[ScheduleModel alloc]init];
if(![schMod isScheduleActiveNow]){
[locationManager stopUpdatingLocation];
NSLog(@"LocationManager OFF");
} else if(!locationManager){
[locationManager startUpdatingLocation];
NSLog(@"LocationManager ON");
}
[schMod release];
}

但它只适用于 LocationManager正在工作中。当我切换 LocationManager关闭,似乎没有什么可以触发操作 doInBackground .

有没有解决这个问题的方法?

最佳答案

当应用程序暂停时,如果您没有注册后台位置服务并启动位置管理器实例更新,则无法通过计时器让应用程序再次开始更新。
您的选择是:

  • 让位置管理器保持更新,但仅在您打算激活的时间响应事件。
  • 仅在进入后台状态时更改为重要的位置更改更新。该应用程序将在位置发生重大变化时唤醒,然后您可以检查时间并根据需要通过开始持续更新来做出响应。
  • 设置一个受监控的区域,在退出该区域时唤醒并做出相应的响应。

  • 在后台持续更新,如果不是必需的,也不是对用户电池的负责任的处置。如果你解释更多你想要完成的事情,我可能会有更好的建议。

    您可以在 github 上的处理程序中看到我在后台状态下所做的一些工作:

    TTLocationHandler

    关于iphone - 在 iOS 中暂停时启动 locationManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12280048/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com