【问题标题】:Stop location updates when app terminate应用程序终止时停止位置更新
【发布时间】:2014-09-06 20:18:37
【问题描述】:

我正在开发一个应用程序,当您在推荐地点附近时会发送通知。 我的问题是当我进入后台然后退出应用程序时,我不希望定位服务在应用程序不工作时工作(但我希望它们在后台工作)。

我只看到 3 个应用程序在关闭时关闭 GPS,我想知道他们是如何做到的,Facebook、谷歌地图和苹果地图,而不是 Foursquare,而不是 FieldTrips...

谢谢大家。

【问题讨论】:

  • 据我了解,您想在应用退出时删除位置,但在后台运行位置。对于applicationWillTerminate 中的AppDelegate 方法。您需要添加stopUpdatingLocationstopMonitoringSignificantLocationChanges 并将位置对象设置为nil。也许它会帮助你。
  • 对不起,没那么容易...当您关闭应用程序并处于后台时,它没有触发 applicationWillTerminate...
  • 哦,现在我可以理解你的问题了。 :)

标签: ios background gps location terminate


【解决方案1】:

您可以为 UIApplicationWillTerminateNotification 添加一个观察者,您可以在其中启动 locationManager 并停止位置更新

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationWillTerminate:)
                                                 name:UIApplicationWillTerminateNotification
                                               object:nil];

收到通知时执行的方法

- (void)applicationWillTerminate:(NSNotification *)notification {
//stop location updates
}

【讨论】:

  • 我试过了,但不幸的是,这并不容易......当您关闭应用程序并处于后台时,它没有发送该通知。
  • @JavierPeigneux 你测试的方法是什么?我看到 -(void)applicationWillTerminate:(UIApplication *)applicationUIApplicationWillTerminateNotification 在其他应用程序处于前台时被调用,我们通过向上滑动快照来关闭我们的应用程序。
  • @AlexPeda 问题是当应用程序在后台,然后你通过向上滑动快照来关闭它...我用日志、断点测试了它,我有代码来停止 gps在其余代码中工作,但它永远不会在那里工作......
  • 我已经发布了该主题的新答案。希望,这会有所帮助。祝你好运:)
  • 查看这篇文章:stackoverflow.com/questions/9077105/…,OP 问题中的第二点是您所描述的问题?如果是这样,接受的答案应该解决它
【解决方案2】:

由于@GuyS 第二个帖子,我找到了我的问题的正确答案:

在你的 AppDelegate.m applicationDidEnterBackground 中添加它

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];
    if ([app respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) {
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            // Synchronize the cleanup call on the main thread in case
            // the task actually finishes at around the same time.
            dispatch_async(dispatch_get_main_queue(), ^{
                if (bgTask != UIBackgroundTaskInvalid)
                {
                    [app endBackgroundTask:bgTask];
                    bgTask = UIBackgroundTaskInvalid;
                }
            });
        }];
    } 
}

并声明该变量:

UIBackgroundTaskIdentifier bgTask;

之后,您只需在 applicationWillTerminate 中停止您的位置服务...

感谢您的回复。

【讨论】:

  • 这应该是一个可接受的答案,它解决了应用程序在后台时调用 applicationWillTerminate 的问题。
【解决方案3】:

@GuyS 在本主题中提供的解决方案应该可以工作。如果应用程序在后台,我会收到UIApplicationWillTerminateNotification,然后我通过向上滑动快照来关闭它。请检查您是否正确使用NSNotificationCenter(尤其是添加和删除通知)。另外,请在应用处于后台时检查您在通知上订阅的对象是否处于活动状态。

另一个类似的解决方案是将禁用 GPS 的代码放在您的 AppDelegate 方法中的适当 UIApplicationDelegate 回调中。

- (void)applicationWillTerminate:(UIApplication *)application {
//stop location updates
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 2017-07-30
    • 2011-11-16
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多