首页 > 解决方案 > 在调试器中出现错误:此应用已尝试在没有使用说明的情况下访问隐私敏感数据

问题描述

我在调试区域遇到问题。它的意思是:“这个应用程序试图在没有使用说明的情况下访问隐私敏感数据。应用程序的 Info.plist 必须包含“NSLocationAlwaysAndWhenInUseUsageDescription”和“NSLocationWhenInUseUsageDescription”键,并带有向用户解释应用程序如何使用这些数据的字符串值。 "

我正在创建的应用程序涉及仅在他/她使用该应用程序时获取用户的位置,或者换句话说,仅在前台获取用户的位置。我只在 info.plist 中添加了:密钥(隐私 - 使用时的位置使用说明)、类型(字符串)、值(我们将使用您的位置来寻找您附近的工作!)。

这是我的视图控制器中的代码:

import UIKit
import Foundation
import CoreLocation

class JobTableViewController: UITableViewController, CLLocationManagerDelegate {
    
let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
// Implement Indeed Job Search API
    let headers = [
        "x-rapidapi-host": "indeed-indeed.p.rapidapi.com",
        "x-rapidapi-key": "838897ae8cmsha8fef9af0ee840dp1be982jsnf48d2de3c84a"
    ]

    let request = NSMutableURLRequest(url: NSURL(string: "https://indeed-indeed.p.rapidapi.com/apigetjobs?v=2&format=json")! as URL,
                                            cachePolicy: .useProtocolCachePolicy,
                                        timeoutInterval: 10.0)
    request.httpMethod = "GET"
    request.allHTTPHeaderFields = headers

    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            print(error!)
        } else {
            let httpResponse = response as? HTTPURLResponse
            print(httpResponse!)
        }
    })

    dataTask.resume()
   
    
// Recieve live location from user
    // For use when the app is open
    locationManager.requestWhenInUseAuthorization()
    
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
    }
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let location = locations.first {
        print(location.coordinate)
    }
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if(status == CLAuthorizationStatus.denied) {
        showLocationDisabledPopUp()
        
    }
}

// If user disabled location services
func showLocationDisabledPopUp() {
    let alertController = UIAlertController(title: "Location Access is Disabled", message: "In order to automatically find jobs near you, we need your location.", preferredStyle: .alert)
    
    let cancelAction = UIAlertAction(title: "Okay", style: .cancel, handler: nil)
    alertController.addAction(cancelAction)
    
    let openAction = UIAlertAction(title: "Open Settings", style: .default) { (action) in
        if let url = URL(string: UIApplication.openSettingsURLString) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }
    alertController.addAction(openAction)
    
    self.present(alertController, animated: true, completion: nil)
    
}

我能够成功构建应用程序并登录到应用程序。调试器甚至成功地显示了模拟器的坐标。但是,它仍然给我这个警告,我无法使用坐标通过我安装的 API 查找附近的工作(代码也包含在上面)。我不知道为什么它甚至会警告我“NSLocationAlwaysAndWhenInUseUsageDescription”,因为我的程序中没有提到过一次。

这个警告有正当理由吗?此外,此警告是否与应用程序无法通过 API 提供附近工作的事实有关?我知道这一切都非常令人困惑,所以我附上了下面所有内容的截图。请提出任何问题以进行澄清,非常感谢您的所有帮助!

info.plist screenshot 调试器截图

标签: iosswiftxcodeerror-handlinglocation

解决方案


调试器消息和其他两个答案告诉您需要做什么。添加具有相同文本的附加使用密钥。

为什么?这是必需的,因为从 iOS 12 开始,用户可以在询问“始终”时回复“使用时”,而在 iOS 13 中,只有在应用程序首次询问“始终”时才会询问“使用时”。

在 iOS 12 之前,根据您的应用要求的内容,使用不同的“使用时”和“始终”权限请求字符串。

在 iOS 12 及更高版本中,您需要在单个键中的“始终”和“使用时”场景中都有一个使用字符串。

Apple 提供了新NSLocationAlwaysAndWhenInUseUsageDescription密钥,让应用程序开发人员有机会根据新行为提供不同的信息。

从理论上讲,这是 iOS 12 之后唯一需要的密钥,但为了向后兼容,您需要同时包含旧密钥和新密钥,即使您的最低 ios 目标是 iOS 12 或更高版本。


推荐阅读