ios - 在调试器中出现错误 : This app has attempted to access privacy-sensitive data without a usage description

标签 ios swift xcode error-handling location

我在调试区域遇到问题。它的意思是:“这个应用程序试图在没有使用描述的情况下访问隐私敏感数据。应用程序的 Info.plist 必须包含“NSLocationAlwaysAndWhenInUseUsageDescription”和“NSLocationWhenInUseUsageDescription”键,其中包含向用户解释应用程序如何使用这些数据的字符串值。 "
我正在创建的应用程序涉及仅在他/她使用该应用程序时获取用户的位置,或者换句话说,仅在前台获取用户的位置。我只在 info.plist 中添加了: key (隐私 - 使用时的位置使用说明)、类型(字符串)、值(我们将使用您的位置来寻找您附近的工作!)。
这是我的 View Controller 中的代码:

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
debugger screenshot

最佳答案

调试器消息和其他两个答案告诉您需要做什么。添加具有相同文本的附加使用 key 。
为什么?这是必需的,因为从 iOS 12 开始,用户可以在询问“始终”时回复“使用时”,而在 iOS 13 中,只有在应用程序首次询问“始终”时才会询问“使用时”。
在 iOS 12 之前,根据您的应用要求的内容,使用不同的“使用时”和“始终”权限请求字符串。
在 iOS 12 及更高版本中,您需要在单个键中的“始终”和“使用时”场景中都有一个使用字符串。
Apple 提供了新的 NSLocationAlwaysAndWhenInUseUsageDescription让应用程序开发人员有机会根据新行为提供不同信息的关键。
从理论上讲,这是 iOS 12 之后唯一需要的 key ,但为了向后兼容,您需要同时包含旧 key 和新 key ,即使您的最低 ios 目标是 iOS 12 或更高版本。

关于ios - 在调试器中出现错误 : This app has attempted to access privacy-sensitive data without a usage description,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62616010/

相关文章:

ios - 创建 NSManagedObject 属性值的副本

iphone : Settings. 包返回空值

ios - TextField 宽度永远不会小于其带有占位符文本的宽度

ios - Xcode 设备模拟器文件

ios - 如何在 Swift 3.0 中一段时间​​后插入警报

iphone - 当发布的App对User Interaction无响应时,有什么好的方法可以获取有用的信息?

ios - 访问 iCloud 驱动器上的应用程序数据

ios - Swift 中 UIViewController 的子类成员的双重初始化

ios - 如何在所有 View 中拥有一个 UIViewController 但不与所有 View 相关

swift - 在 Swift 5 中禁用 HTTPS GET 证书检查