【问题标题】:NSLocationWhenInUseUsageDesciption Missing from Info.plistInfo.plist 中缺少 NSLocationWhenInUseUsageDescription
【发布时间】:2019-04-03 00:54:39
【问题描述】:

下午好, 在过去的几周里,我在 Xcode 中遇到了一个问题,上面写着:

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

我在 info-plist 中实现了这两种用法描述,我尝试从手机中删除应用程序(我将 iPhone 用作模拟器)我尝试重新组织我的代码,我尝试注释掉特定的线只是为了查看消息是否会消失并让我看到我的位置。我已经尝试删除并重新安装谷歌地图窗格,但没有。我尝试在 StackOverflow、medium 和 GitHub 上阅读有关此问题的内容,以尝试查看是否可以使用以前的技巧来帮助解决我的问题。我什至在这里发帖,看看我是否可以对这个问题有所了解。

我不知道该怎么做才能解决这个问题,我真的不想重新开始。我将在下面发布我的全部代码,这非常广泛。如果有人有空闲时间通读并让我知道我做错了什么或没有实施,将不胜感激。

import UIKit
import GoogleMaps
import GooglePlaces
import CoreLocation

class mainViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, GMSAutocompleteViewControllerDelegate, UITextFieldDelegate {

    let currentLocationMarker = GMSMarker()
    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.prefersLargeTitles = false

        navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 25)]


        myMapView.delegate=self
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
        locationManager.startMonitoringSignificantLocationChanges()
        locationManager.startUpdatingLocation()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        setupViews()
        initGoogleMaps()
        txtFieldSearch.delegate=self

    func initGoogleMaps() {
        let camera = GMSCameraPosition.camera(withLatitude: 40.014281, longitude: -83.030914, zoom: 17.0)
    self.myMapView.camera = camera
        self.myMapView.delegate = self
        self.myMapView.isMyLocationEnabled = true
    }
    func getLocation() {

        let status  = CLLocationManager.authorizationStatus()

        if status == .notDetermined {
            locationManager.requestWhenInUseAuthorization()
            return
        }

        if status == .denied || status == .restricted {
            let alert = UIAlertController(title: "Location Services Disabled", message: "Please enable Location Services in Settings", preferredStyle: .alert)

            let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alert.addAction(okAction)

            present(alert, animated: true, completion: nil)
            return
        }
    }



    @objc func btnMyLocationAction() {
        let location: CLLocation? = myMapView.myLocation
        if location != nil {
            myMapView.animate(toLocation: (location?.coordinate)!)
        }
    }

    let myMapView: GMSMapView = {
        let v=GMSMapView()
        v.translatesAutoresizingMaskIntoConstraints=false
        return v
    }()


    let btnMyLocation: UIButton = {
        let btn=UIButton()
        btn.backgroundColor = UIColor.white
        btn.setImage(#imageLiteral(resourceName: "my_location-1"), for: .normal)
        btn.layer.cornerRadius = 25
        btn.clipsToBounds=true
        btn.tintColor = UIColor.gray
        btn.imageView?.tintColor=UIColor.gray
        btn.addTarget(self, action: #selector(btnMyLocationAction), for: .touchUpInside)
        btn.translatesAutoresizingMaskIntoConstraints=false
        return btn
    }()
}

【问题讨论】:

    标签: swift xcode google-maps showuserlocation


    【解决方案1】:

    搜索“info.plist”的应用程序目标的构建设置,并仔细研究完整路径。确保您将密钥添加到正确的 info.plist - 它是实际正式用作应用程序构建的 info.plist 的密钥。您还可以单击 Build Settings 旁边的 Info 选项卡以验证这两个条目是否存在。

    它应该是这样的(只需确保为两者都提供一个值):

    【讨论】:

    • 谢谢。 @smartcat 允许我接收推送通知,以允许用户决定他或她是否希望允许应用访问他们的位置。但它不允许我在地图上显示他们的位置
    • 我想在允许应用访问他们的位置后它会显示。
    • 好吧,您要么没有显示所有代码,要么忘记覆盖 CLLocationManagerDelegate 的方法。您的类被设置为委托,在这一行中:locationManager.delegate = self,但您需要实际覆盖这些方法。 This SO 有一些例子。如果您遇到进一步的问题,请发布一个新问题,因为听起来这个问题现在已经解决了。
    • 谢谢@Smartcat。我什至不需要显示我的代码。我真的希望其他有同样问题的人可以从中学习!
    • :) 很高兴能帮上忙!
    【解决方案2】:

    所有原因都是因为您需要添加描述才能向用户询问位置权限。为了做到这一点,我们必须去编辑属性列表:

    在 Xcode 的 Supporting Files 文件夹下,我们将在 Info.plist 中添加两个新键('Privacy-Location Always And When In Use Usage Description' 和 'Privacy-Location When In Use Usage Description') .

    为了做到这一点,您转到显示“信息属性列表”的部分,单击那个小添加按钮 (+)。如果您删除“应用程序类别”并开始输入“隐私”(大写字母 P),您将开始看到弹出的建议。

    在找到并选择“隐私-位置始终和使用时使用说明”和“隐私-位置使用时使用说明”后,接下来您要做的就是为它们中的每一个赋予一个值。在“价值”列下,只需写下该描述“我们需要您的位置来……”(……也许是为了获取您当前的天气状况)。

    现在您有两个新键,它们每个都有一个数据类型为字符串的值。

    【讨论】:

    • 在我的 info-plist 中,我拥有所有必需的信息,但由于某种原因,它没有链接到构建设置旁边的 info 中。我不得不把它放在自定义 IOS 目标属性中
    • 它不允许你在应用程序上显示他们的位置或其他东西?
    • 不幸的是,这并没有让我失望。它甚至不会向我发送请求许可的弹出通知,它只是说我必须将其添加到我的 info-plist 中。将其添加到自定义目标后,它允许我!有点偏离,就像蓝点偏离屏幕中间而不是右侧,但我会活下去,哈哈。
    • 我认为您应该创建一个新项目并按照上面的说明重新开始,但不是今天,明天开始工作。相信我,这会对你有很大帮助。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 2022-06-30
    • 2017-11-28
    • 1970-01-01
    • 2018-10-13
    • 2021-09-24
    • 1970-01-01
    • 2015-11-07
    • 2017-01-26
    相关资源
    最近更新 更多