定位服务将在 iOS 8 后进入“非活动”状态
Location service going to “Inactive” state in iOS 8
我正在创建一个跟踪用户的应用程序。当用户登录到应用程序时,我已经开始进行重要的位置更新。然后我把我的应用程序放在后台。然后开始开着我的车旅行。在 500m-1km 之后,我的位置发生了重大变化。在获得更新时,我已经停止了重要的位置更新并启动了标准 GPS。在我看到这个 "Location icon should now be in state 'Inactive'" 之后,它会提供 2-3 分钟的位置更新,而我的应用程序没有获得任何位置更新。
如果应用程序处于前台状态,则应用程序会再次开始获取位置更新。
我正在使用共享位置实例。我有 iPhone 5 和 iOS 8.1.3。我正在使用 Swift.
添加一些代码..我已经这样做了
gpsManager = CLLocationManager()
gpsManager.delegate = self
gpsManager.desiredAccuracy = kaccuracy
gpsManager.distanceFilter = kdistanceFilter
gpsManager.pausesLocationUpdatesAutomatically = false
gpsManager.headingFilter = kheadingFilter
gpsManager.activityType = kactivityType
isTracking = false
isMonitoringSignificantChanges = false
//on login
var sharedGPSManager=GeoLocation.sharedInstance
sharedGPSManager.delegate=self
let status=sharedGPSManager.authorizationStatus()
if(status == 0)
{
NSLog("Allowed to track")
}
else
{
NSLog("Not allowed")
}
sharedGPSManager.startSignificantLocationTracking()
func gpsManager(manager:GeoLocation, didUpdateLocations locations:AnyObject)
{
var sharedGPSManager=GeoLocation.sharedInstance
sharedGPSManager.stopSignificantLocationTracking()
task=UIApplication.sharedApplication().beginBackgroundTaskWithName("wifiUpload", expirationHandler: { () -> Void in
}
sharedGPSManager.delegate = self
let status = sharedGPSManager.authorizationStatus()
if(status == 0)
{
NSLog("Allowed to track")
}
else
{
NSLog("Not allowed")
}
sharedGPSManager.startTracking()
//process location data
endBackgroundTask(task)
}
// 开始位置追踪
func startTracking(){
if(isMonitoringSignificantChanges){
gpsManager.stopMonitoringSignificantLocationChanges()
}
gpsManager.stopUpdatingLocation()
if CLLocationManager.headingAvailable()
{
gpsManager.startUpdatingHeading()
}
gpsManager.startUpdatingLocation()
isTracking=true
isMonitoringSignificantChanges=false
}
您需要在后台模式下添加 Location Update
功能
Project Target > Capabilities > 打开后台模式 ON > 勾选所有适用于您的应用程序的选项
您必须在 info.plist 中启用必需的后台模式,如 "App registers for location updates" 然后您的应用程序将在后台为您提供位置更新。
而且您必须使用 self.locationManager.startUpdatingLocation()
而不是重要的位置更新。
最后我得到了关于 Apple 开发者的答案 forum.keeping 重要位置总是对我有用....
我正在创建一个跟踪用户的应用程序。当用户登录到应用程序时,我已经开始进行重要的位置更新。然后我把我的应用程序放在后台。然后开始开着我的车旅行。在 500m-1km 之后,我的位置发生了重大变化。在获得更新时,我已经停止了重要的位置更新并启动了标准 GPS。在我看到这个 "Location icon should now be in state 'Inactive'" 之后,它会提供 2-3 分钟的位置更新,而我的应用程序没有获得任何位置更新。
如果应用程序处于前台状态,则应用程序会再次开始获取位置更新。
我正在使用共享位置实例。我有 iPhone 5 和 iOS 8.1.3。我正在使用 Swift.
添加一些代码..我已经这样做了
gpsManager = CLLocationManager()
gpsManager.delegate = self
gpsManager.desiredAccuracy = kaccuracy
gpsManager.distanceFilter = kdistanceFilter
gpsManager.pausesLocationUpdatesAutomatically = false
gpsManager.headingFilter = kheadingFilter
gpsManager.activityType = kactivityType
isTracking = false
isMonitoringSignificantChanges = false
//on login
var sharedGPSManager=GeoLocation.sharedInstance
sharedGPSManager.delegate=self
let status=sharedGPSManager.authorizationStatus()
if(status == 0)
{
NSLog("Allowed to track")
}
else
{
NSLog("Not allowed")
}
sharedGPSManager.startSignificantLocationTracking()
func gpsManager(manager:GeoLocation, didUpdateLocations locations:AnyObject)
{
var sharedGPSManager=GeoLocation.sharedInstance
sharedGPSManager.stopSignificantLocationTracking()
task=UIApplication.sharedApplication().beginBackgroundTaskWithName("wifiUpload", expirationHandler: { () -> Void in
}
sharedGPSManager.delegate = self
let status = sharedGPSManager.authorizationStatus()
if(status == 0)
{
NSLog("Allowed to track")
}
else
{
NSLog("Not allowed")
}
sharedGPSManager.startTracking()
//process location data
endBackgroundTask(task)
}
// 开始位置追踪 func startTracking(){
if(isMonitoringSignificantChanges){
gpsManager.stopMonitoringSignificantLocationChanges()
}
gpsManager.stopUpdatingLocation()
if CLLocationManager.headingAvailable()
{
gpsManager.startUpdatingHeading()
}
gpsManager.startUpdatingLocation()
isTracking=true
isMonitoringSignificantChanges=false
}
您需要在后台模式下添加 Location Update
功能
Project Target > Capabilities > 打开后台模式 ON > 勾选所有适用于您的应用程序的选项
您必须在 info.plist 中启用必需的后台模式,如 "App registers for location updates" 然后您的应用程序将在后台为您提供位置更新。
而且您必须使用 self.locationManager.startUpdatingLocation()
而不是重要的位置更新。
最后我得到了关于 Apple 开发者的答案 forum.keeping 重要位置总是对我有用....