调试器中出现错误:此应用程序尝试在没有使用说明的情况下访问隐私敏感数据

3 error-handling xcode location ios swift

我在调试区域遇到问题。它说:“此应用程序试图在没有使用说明的情况下访问隐私敏感数据。该应用程序的 Info.plist 必须包含 \xe2\x80\x9cNSLocationAlwaysAndWhenInUseUsageDescription\xe2\x80\x9d 和 \xe2\ x80\x9cNSLocationWhenInUseUsageDescription\xe2\x80\x9d 键,其中包含向用户解释应用程序如何使用此数据的字符串值。

\n

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

\n

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

\n
import UIKit\nimport Foundation\nimport CoreLocation\n\nclass JobTableViewController: UITableViewController, CLLocationManagerDelegate {\n    \nlet locationManager = CLLocationManager()\n\noverride func viewDidLoad() {\n    super.viewDidLoad()\n    // Do any additional setup after loading the view.\n    \n// Implement Indeed Job Search API\n    let headers = [\n        "x-rapidapi-host": "indeed-indeed.p.rapidapi.com",\n        "x-rapidapi-key": "838897ae8cmsha8fef9af0ee840dp1be982jsnf48d2de3c84a"\n    ]\n\n    let request = NSMutableURLRequest(url: NSURL(string: "https://indeed-indeed.p.rapidapi.com/apigetjobs?v=2&format=json")! as URL,\n                                            cachePolicy: .useProtocolCachePolicy,\n                                        timeoutInterval: 10.0)\n    request.httpMethod = "GET"\n    request.allHTTPHeaderFields = headers\n\n    let session = URLSession.shared\n    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in\n        if (error != nil) {\n            print(error!)\n        } else {\n            let httpResponse = response as? HTTPURLResponse\n            print(httpResponse!)\n        }\n    })\n\n    dataTask.resume()\n   \n    \n// Recieve live location from user\n    // For use when the app is open\n    locationManager.requestWhenInUseAuthorization()\n    \n    if CLLocationManager.locationServicesEnabled() {\n        locationManager.delegate = self\n        locationManager.desiredAccuracy = kCLLocationAccuracyBest\n        locationManager.startUpdatingLocation()\n    }\n}\nfunc locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {\n    if let location = locations.first {\n        print(location.coordinate)\n    }\n}\n\nfunc locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {\n    if(status == CLAuthorizationStatus.denied) {\n        showLocationDisabledPopUp()\n        \n    }\n}\n\n// If user disabled location services\nfunc showLocationDisabledPopUp() {\n    let alertController = UIAlertController(title: "Location Access is Disabled", message: "In order to automatically find jobs near you, we need your location.", preferredStyle: .alert)\n    \n    let cancelAction = UIAlertAction(title: "Okay", style: .cancel, handler: nil)\n    alertController.addAction(cancelAction)\n    \n    let openAction = UIAlertAction(title: "Open Settings", style: .default) { (action) in\n        if let url = URL(string: UIApplication.openSettingsURLString) {\n            UIApplication.shared.open(url, options: [:], completionHandler: nil)\n        }\n    }\n    alertController.addAction(openAction)\n    \n    self.present(alertController, animated: true, completion: nil)\n    \n}\n
Run Code Online (Sandbox Code Playgroud)\n

我能够成功构建应用程序并登录该应用程序。调试器甚至成功地显示了模拟器的坐标。但是,它仍然向我发出此警告,并且我无法通过我安装的 API(代码也包含在上面)使用坐标来查找附近的工作。我不知道为什么它甚至警告我有关“NSLocationAlwaysAndWhenInUseUsageDescription”的信息,因为它在我的程序中没有被提及过。

\n

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

\n

info.plist 屏幕截图\n调试器屏幕截图

\n

Pau*_*w11 6

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

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

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

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

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

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