首页 文章

背景中的ios(swift)位置(ios 9.3.2)停止工作

提问于
浏览
2

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

locationManager(manager:CLLocationManager,didUpdateLocations locations:[CLLocation]){}

在后台(应用程序关闭)的某些(随机)时间后停止更新位置 . 在iphone 4s ios 8.4上,此问题不存在 .

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

3 回答

  • 0

    我有同样的问题 . 我找到解决这个问题的方法是去 settings - > privacy - > location services - >找到你的应用 - > allow location access - >切换到 never - >回到 location services - >然后 allow location access 再次打开你的服务 . 但有时只能工作 .

    另外,据我所知(可能我很担心 . )如果准确度不够你所设定的 . 位置更新不会被激活 .

  • 0

    我解决了它每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 中有两个键( NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

    <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
    
  • 0

    此外,您还可以使用重要的更改位置服务,使用此服务,即使您暂停,iOS也会唤醒您的应用

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

相关问题