ios - xcode MKMapView 确实失败并出现错误:Error Domain=kCLErrorDomain Code=1 "(null)"
问题描述
我在 xcode 中编写地图时遇到了这些错误:
2021-01-09 19:02:48.228694+0100 BudapestBoards[31795:558225] Metal API Validation Enabled
2021-01-09 19:02:48.433777+0100 BudapestBoards[31795:558225] This app has attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an “NSLocationWhenInUseUsageDescription” key with a string value explaining to the user how the app uses this data
2021-01-09 19:02:50.483788+0100 BudapestBoards[31795:558499] [MKCoreLocationProvider] CLLocationManager(<CLLocationManager: 0x600002b2b5b0>) for <MKCoreLocationProvider: 0x600001b30360> did fail with error: Error Domain=kCLErrorDomain Code=1 "(null)"
CoreSimulator 732.18.6 - Device: iPhone 12 Pro Max (B1F529FE-C1E7-4C0A-B918-A3C76E006F27) - Runtime: iOS 14.3 (18C61) - DeviceType: iPhone 12 Pro Max
我正在使用地图视图。我的代码是:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.delegate = self
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
manager.stopUpdatingLocation()
render(location)
}
}
func render(_ location: CLLocation) {
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: coordinate, span: span)
mapView.setRegion(region, animated: true)
}
}
}
感谢您在添加 NSLocationWhenInUseUsageDescription 时做出回应,它说它已经存在于字典中,如果我想替换它。如果我按下替换它除了删除该行之外什么都不做。
解决方案
此错误中的相关信息是:
此应用程序试图在没有使用说明的情况下访问隐私敏感数据。应用程序的 Info.plist 必须包含一个“NSLocationWhenInUseUsageDescription”键和一个字符串值,向用户解释应用程序如何使用这些数据
您需要在您的文件中提供一个字符串Info.plist
来解释您的应用为什么要访问位置信息。
所以找到Info.plist
文件,添加一个以“ NSLocationWhenInUseUsageDescription
”为键的新行和一个文本。该文本显示在询问用户是否允许访问的警报中,因此请考虑对其进行本地化。
推荐阅读
- java - 如何在从数据源创建新连接之前执行自定义代码
- python - Dataframe 具有字符串列,其中包含不同格式的日期值。如何将整列数据转换为单个日期格式?
- android - Android 图片上传在 Localhost 但不在在线服务器上
- javascript - 在打字稿中声明对象中的所有属性相同类型?
- javascript - 如何对调用私有方法的公共方法进行单元测试
- java - 如何使用 DateTimeFormatter 从 MONTH("MMMM") 或 YEAR("yyyy") 获取日期对象
- reactjs - 如何在不获取对象的情况下从反应引导表中导出数据
- sqlite - 在 PowerBasic sqlite3 Wrapper 中从 ASCIIZ PTR 创建 BSTR
- php - PHP - 返回计数一个项目出现在数组中
- python-3.x - Jupyter notebook Python 内核 - FileNotFoundError: [Errno 2] No such file or directory python3