我有一个应用程序,做定期的位置更新,以映射用户的路径。
我正在使用延迟更新。
if (CLLocationManager.deferredLocationUpdatesAvailable() == true && _isDeferingUpdates == false)
{
print("Doing refresh")
_isDeferingUpdates = true
_locationManager.allowDeferredLocationUpdatesUntilTraveled(C_GPS.ACTIVITY_UPDATE_DISTANCE, timeout: C_GPS.ACTIVITY_UPDATE_TIME)
} else
{
print("Could not refresh")
// iPhone 4S does not have deferring so must keep it always on
_locationManager.startUpdatingLocation()
}当应用程序打开时,我每秒钟都会收到“进行刷新”的调用。
我的设置:
在我的pList NSLocationWhenInUseUsageDescription && NSLocationAlwaysUsageDescription上有两个键
打开后台模式,以进行远程通知和位置更新。
有地图设置使用自行车和行人。
将我手机上的所有权限设置为“是”。
当我的应用程序进入后台时,你知道为什么我的延迟更新失败的其他原因吗?
它从来都不起作用,苹果文档也没有多大帮助。
由于未知的原因,位置管理器DeferredFailed没有进入延迟模式。如果GPS不可用、非活动或暂时中断,则可能发生此错误。如果在有GPS硬件的设备上出现此错误,解决方案是再试一次。
下面是我的错误处理程序:
func locationManager(manager: CLLocationManager, didFinishDeferredUpdatesWithError error: NSError?)
{
print("FINISHED BACKGROUND UPDATE with error \(error)")
if (error != nil)
{
print("ERROR IS VALID as CLError")
if (error!.code == CLError.LocationUnknown.rawValue)
{
print("Error: Location Unknown")
} else if (error!.code == CLError.DeferredAccuracyTooLow.rawValue)
{
print("Error: Accuracy too low")
} else if (error!.code == CLError.DeferredFailed.rawValue)
{
print("Error: Deferring Failed")
} else if (error!.code == CLError.Denied.rawValue)
{
print("Error: Denied")
} else
{
print("Error not handled")
}
}
_isDeferingUpdates = false
}发布于 2016-01-07 23:14:38
iOS 9.2,Xcode 7.2,启用ARC
这个问题很可能与您为延迟所选择的距离和时间间隔有关。尝试利用CLLocationDistanceMax和CLTimeIntervalMax对函数调用进行故障排除。例如,将距离设置为CLLocationDistanceMax,然后改变时间间隔,然后尝试相反。
但我看到还有其他事情你可能想改变..。
CLLocationManager.deferredLocationUpdatesAvailable() == true条件,以允许延迟位置更新。_locationManager.startUpdatingLocation()。allowsBackgroundLocationUpdates。_locationManager.startUpdatingLocation()是在Swift中等效的- (void)applicationWillResignActive:(UIApplication *)application方法中生成的,而不是在- (void)applicationDidEnterBackground:(UIApplication *)application方法中生成的。_locationManager.allowDeferredLocationUpdatesUntilTraveled(C_GPS.ACTIVITY_UPDATE_DISTANCE, timeout: C_GPS.ACTIVITY_UPDATE_TIME),这相当于Swift的- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations方法。另外,请注意,在检查错误是否存在之前,您正在打印错误,这可能会导致错误报告。
希望这有帮助,我不会太晚!干杯。
如果这不起作用,那么请张贴更多的您的代码,即您在哪里调用这些功能的应用程序。委托*.m文件。
发布于 2016-01-27 20:42:44
这个错误是有误导性的。问题是4S不支持背景更新。因此,对于4S,我手动引用更新如下:
private func refreshUpdateDeferral()
{
if (CLLocationManager.deferredLocationUpdatesAvailable() == false && _isDeferingUpdates == false)
{
print("Doing deferred referral refresh")
_isDeferingUpdates = true
_locationManager.allowDeferredLocationUpdatesUntilTraveled(C_GPS.ACTIVITY_UPDATE_DISTANCE, timeout: C_GPS.ACTIVITY_UPDATE_TIME)
}
}问题是我有CLLocationManager.deferredLocationUpdatesAvailable() == true,这意味着在已经允许延迟的情况下,我在推迟更新。这似乎会导致上述错误。
因此,如果您收到此错误,请检查您是否是allowDeferredLocationUpdatesUntilTraveled,而deferredUpdates已经处于活动状态!
发布于 2016-01-28 11:30:51
如果您的精度和距离筛选设置不正确,则延迟更新可能会遇到麻烦。假设您的设备支持延迟更新(CLLocationManager.deferredLocationUpdatesAvailable),,您必须首先将您的位置管理器设置为:
desiredAccuracy = kCLLocationAccuracyBest
distanceFilter = kCLDistanceFilterNone然后您可以启动位置管理器并允许延迟更新。
https://stackoverflow.com/questions/32916365
复制相似问题