【问题标题】:Significant Location updates after force quit in iOS app在 iOS 应用程序中强制退出后的重要位置更新
【发布时间】:2015-01-25 11:32:24
【问题描述】:

我目前正在跟踪用户每行驶一英里的位置更新。而且我为应用程序打开了后台模式以查找位置更新。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: 

[NSObject: AnyObject]?) -> Bool {
        locationManager.delegate = self

        if iOS8 {
            locationManager.requestAlwaysAuthorization()
        } else {
            locationManager.startUpdatingLocation()
            locationManager.stopUpdatingLocation()
        }

        locationManager.distanceFilter = 1609.34 // meters in 1 mile
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.pausesLocationUpdatesAutomatically = false
        locationManager.startUpdatingLocation()

        return true
    }

但是,我想知道,如果应用程序已被强制退出,我仍然希望应用程序更新位置。 startUpdatingLocation 类有可能吗?或者我应该使用startMonitoringSignificantLocationChanges

我阅读了文档here,但不太明白何时/当应用程序被强制退出时从startUpdatingLocation 移动到startMonitoringSignificantLocationChanges。应该在applicationWillTerminate函数下吗?

或者,如果这是可能的,或者我应该做些什么。

更新:

我读过here

在大多数情况下,系统在应用程序被强制后不会重新启动它们 由用户退出。一个例外是位置应用程序,它在 iOS 8 和 后来被用户强制退出后重新启动。其他 但是,在某些情况下,用户必须明确启动应用程序或重新启动 应用程序可以在设备启动前自动进入 系统后台。

如果是这样,我应该使用startUpdatingLocation 还是转到startMonitoringSignificantLocationChanges

【问题讨论】:

  • 有兴趣。似乎如果用户配置了位置权限“始终”,系统会重新启动位置应用程序。
  • 文档说“如果应用程序因用户强制退出以外的任何原因终止,系统会在以下事件之一发生时启动应用程序:”。我认为这意味着系统只有在系统强行终止应用程序时才会重新启动应用程序。如果用户退出应用,应用将不会重新启动。
  • @EIMEI - 用户可以通过向上滑动强制退出应用程序 - 没有其他“退出”选项,因此在定位服务的情况下,iOS 将重新启动需要时应用程序,无论它是如何/为何终止的
  • @Paulw11 那么,如果应用程序被用户强制退出(向上滑动),系统将重新启动并且仍会更新位置?
  • 在 ios8 上是的,根据问题中引用的文档

标签: ios ios7 swift ios8 cllocationmanager


【解决方案1】:

如果应用程序被终止,您将无法为该应用程序运行任何程序。

当应用程序终止时,并不总是处理 applicationWillTerminate 函数。在某些情况下,系统会在没有任何通知的情况下终止应用程序。请阅读有关应用程序生命周期的文档。

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

我认为您希望您的应用程序像守护程序服务一样运行。 iOS 不允许我们这样做,除非设备被越狱。

https://www.chrisalvares.com/blog/7/creating-an-iphone-daemon-part-1/

如果您关心设备电池,您会这样做。

  • startUpdatingLocation 和 stopMonitoringSignificantLocationChanges 在 applicationWillEnterForeground。
  • startMonitoringSignificantLocationChanges 和 stopUpdatingLocation 在 applicationDidEnterBackground。

【讨论】:

  • 您为什么建议在applicationWillResignActive 中的重大位置更改和完整位置更新之间切换? applicationWillEnterForeground: 将是一个更好的方法来做到这一点
  • @EIMEI,是的,但我不担心省电。我们尽可能多地关注位置更新。那么当应用程序被强制退出时,应用程序没有办法调用didUpdateLocations吗?有趣,因为文档不知何故意味着其他。
  • 我们已经检查了这个案例..即使在强制退出之后,我们也会在模拟器中获取位置更新..但是当我们检查 iPhone 时,我们没有得到它。你找到什么有用的了吗?
猜你喜欢
  • 1970-01-01
  • 2013-06-18
  • 2013-07-08
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 2013-09-21
相关资源
最近更新 更多