首页 > 解决方案 > 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 时做出回应,它说它已经存在于字典中,如果我想替换它。如果我按下替换它除了删除该行之外什么都不做。

标签: iosswiftxcodedictionarymobile-development

解决方案


此错误中的相关信息是:

此应用程序试图在没有使用说明的情况下访问隐私敏感数据。应用程序的 Info.plist 必须包含一个“NSLocationWhenInUseUsageDescription”键和一个字符串值,向用户解释应用程序如何使用这些数据

您需要在您的文件中提供一个字符串Info.plist来解释您的应用为什么要访问位置信息。

所以找到Info.plist文件,添加一个以“ NSLocationWhenInUseUsageDescription”为键的新行和一个文本。该文本显示在询问用户是否允许访问的警报中,因此请考虑对其进行本地化。


推荐阅读