【问题标题】:ios(swift) location in background (ios 9.3.2) stops workingios(swift)在后台(ios 9.3.2)位置停止工作
【发布时间】:2016-09-30 23:12:10
【问题描述】:

我不确定这是错误还是某种配置问题,但是(但我将每个选项都放在 info.plist、locationManager.allowsBackgroundLocationUpdates = true 以及我在网上找到的所有其他提示,但问题仍然存在)

locationManager(经理:CLLocationManager,didUpdateLocations 位置:[CLLocation]){ }

在后台(应用关闭)一段时间后(随机)停止更新位置。在 iphone 4s ios 8.4 上不存在此问题。

也许其他人解决了这个问题? 谢谢

【问题讨论】:

  • 你能告诉我们你的代码吗?
  • 你的问题解决了吗。我也面临同样的问题。
  • 我也是。这似乎是无缘无故发生的,如果您在后台注册了某些区域,也请停止跟踪。有人想通了吗?它真的是一个错误吗?在 9.3.1 中,我永远不会遇到这种情况......

标签: ios swift core-location


【解决方案1】:

我解决了它每 3 分钟重新启动一次位置管理器:

在您的委托方法(在我的情况下为locationDidUpdateToLocation)中放置类似

的内容
     self.timeElapsedSinceLastTrackingUpdate = NSDate().timeIntervalSinceDate(self.lastUpdatedTime)
     if timeElapsedSinceLastTrackingUpdate > 180 {
        self.lastUpdatedTime = NSDate()         
        self.LocationMgr.stopUpdatingLocation()
        UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({})                
        NSTimer.scheduledTimerWithTimeInterval(180, target: self.LocationMgr, selector: #selector(self.LocationMgr.startUpdatingLocation), userInfo: nil, repeats: false)
     }

还要确保您的Info.plist 中有两个密钥(NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription)

    <key>NSLocationAlwaysUsageDescription</key>
    <string>This app needs to use your location</string>

    <key>NSLocationWhenInUseUsageDescription</key>
    <string>This app needs to use your location</string>

最后,请务必在位置管理器上设置这些变量:

pausesLocationUpdatesAutomatically = false

和

allowsBackgroundLocationUpdates = true

【讨论】:

  • 我在 iOS 10 上对其进行了测试,但它对我不起作用。一段时间后(不知道确切的时间)它在后台停止获取位置。
  • 这里也一样,在我的情况下,它在 15 分钟后停止。我在 iPhone6 Plus 和 iOS 10.0.1 上测试过
【解决方案2】:

另外你还可以使用重大变更定位服务,有了这个服务,即使你被挂起,iOS也会唤醒你的应用

查找:startMonitoringSignificantLocationChanges 在: https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/stopMonitoringSignificantLocationChanges

【讨论】:

    【解决方案3】:

    我也遇到了同样的问题。我发现解决此问题的方法是转到 settings -> privacy -> location services -> 找到您的应用程序 -> allow location access -> 切换到 never -> 回到 location services -> 然后allow location access 再次打开您的服务。但有时只工作。

    另外,据我所知(可能我很担心。)如果准确度不足以达到您设置的精度。不会启动位置更新。

    【讨论】:

      猜你喜欢
      • 2017-12-16
      • 2018-02-19
      • 2017-03-26
      • 2019-02-11
      • 1970-01-01
      • 2014-03-04
      • 2014-11-17
      • 2018-01-03
      相关资源
      最近更新 更多