首页 > 解决方案 > NSLocationWhenInUseUsageDescription 错误继续显示

问题描述

我试图在我的项目中实现 MapKit 和 CoreLocation。不幸的是,错误一直显示

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

所以这是我的代码和info.plist

import Foundation
import CoreLocation
import MapKit

class ViewController: UITabBarController, CLLocationManagerDelegate{

    let locationManager: CLLocationManager = CLLocationManager()

    @IBAction func backBarButton(_ sender: Any) {
        self.dismiss(animated: true, completion:nil)
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }
}

作为源代码 info.plist 打开

<key>NSLocationWhenInUseUsageDescription</key>
    <string>accept to get location</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>description</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>description</string>
    <key>NSLocationUsageDescription</key>
    <string>description</string>

打开 .plist 作为属性视图

在此处输入图像描述

我尝试创建一个新项目并且它运行顺利,但是当我尝试在现有项目上实现它时它不起作用。

标签: iosswiftcore-location

解决方案


从 iOS 11 开始,您不能在不提供“When in Use”的情况下请求“Always”:如果您只设置 Privacy – Location Always Usage Description,它将不会出现,并且您将收到错误消息“Info.plist must contain和NSLocationAlwaysAndWhenInUseUsageDescription钥匙NSLocationWhenInUseUsageDescription……”。在 Xcode 9 beta 中,我必须使用NSLocationAlwaysAndWhenInUseUsageDescription密钥;Xcode 不会选择匹配的隐私密钥。

位置经理要求“始终”:

点击链接了解更多描述


推荐阅读