【问题标题】:CLLocationManager didFailWithError: what error stops location updatesCLLocationManager didFailWithError:什么错误会停止位置更新
【发布时间】:2026-02-08 07:35:01
【问题描述】:

iOS CLLocationManager 在其didFailWithError 方法中报告各种错误: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CoreLocationConstantsRef/Reference/reference.html#//apple_ref/c/econst/kCLErrorDenied

在文档中不清楚哪些错误(kCLErrorDeniedkCLErrorLocationUnknown 除外)会停止位置管理器和位置更新?

问候,

【问题讨论】:

标签: ios cllocationmanager cllocation didfailwitherror


【解决方案1】:

尝试打印您的错误对象描述。你会知道失败的原因是什么。可能的原因可能是您没有授予应用使用位置的权限,或者手动撤销了使用位置的权限。

如果您在模拟器中失败,请确保模拟器指向某个自定义位置。

【讨论】:

  • 我确实知道文档中的错误意味着什么,我可以强制其中一些错误(上面提到的两个)。但我的问题是,这些错误中哪些会停止位置管理器,哪些仅用于提供信息,不会停止位置管理器。例如,如果您想记录 GPS 轨迹,这很有趣。
【解决方案2】:

根据我的经验,这些错误不会停止位置管理器。但是,您可能需要使用该函数来处理可能的授权更改

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    // here you call the function where you manage the location authorizations at the launch of the app
}
 

【讨论】: