![](/img/trans.png)
[英]This app has crashed because it attempted to access privacy-sensitive data without a usage description, even with usage description
[英]Getting error in debugger: This app has attempted to access privacy-sensitive data without a usage description
我在調試區域遇到問題。 它是說:“這個應用程序試圖在沒有使用說明的情況下訪問隱私敏感數據。該應用程序的 Info.plist 必須同時包含“NSLocationAlwaysAndWhenInUseUsageDescription”和“NSLocationWhenInUseUsageDescription”鍵以及向用戶解釋應用程序如何使用此數據的字符串值。 “
我正在創建的應用程序涉及僅在他/她正在使用該應用程序時獲取用戶的位置,或者換句話說,僅在前台。 我只在我的 info.plist 中添加了:Key(Privacy - Location When In Use Usage Description), Type(String), Value(We'll use your location to find jobs near you.)。
這是我認為 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 提供附近的工作這一事實有關? 我知道這一切都很混亂,所以我在下面附上了所有內容的截圖。 請提出任何問題以進行澄清,非常感謝您的所有幫助!
調試器消息和其他兩個答案告訴您需要做什么。 添加具有相同文本的附加使用密鑰。
為什么? 這是必需的,因為從 iOS 12 開始,用戶可以在要求“始終”時響應“使用時”,而在 iOS 13 中,只有在應用程序首次要求“始終”時才會被要求“使用時”。
在 iOS 12 之前,根據您的應用程序要求使用不同的“使用時”和“始終”權限請求字符串。
在 iOS 12 及更高版本中,您需要在單個鍵中的“始終”和“使用時”場景中都有一個使用字符串。
Apple 提供了新的NSLocationAlwaysAndWhenInUseUsageDescription
鍵,讓應用程序開發人員有機會根據新行為提供不同的信息。
理論上,這是 iOS 12 之后唯一需要的密鑰,但為了向后兼容,您需要同時包含舊密鑰和新密鑰,即使您的最小 ios 目標是 Z1BDF605991920DB11CBDF128508204C4EBZ 或更高版本。
只需在 info.plist 中添加NSLocationAlwaysAndWhenInUseUsageDescription
和NSLocationWhenInUseUsageDescription
這兩個鍵,位置服務將完全正常啟動。
要從用戶的設備獲取位置更新,您需要征得用戶的許可。 如果未要求,Apple 將在應用程序提交審核時拒絕該應用程序。
在 info.plist 中添加這兩個鍵 -
NSLocationWhenInUseUsageDescription
name - Privacy - Location When In Use Usage Description
reason - why your app wants to access location
NSLocationAlwaysAndWhenInUseUsageDescription
name - Privacy - Location Always and When In Use Usage Description
reason - why your app wants to access location
您可能忘記在 info.plist 中添加Privacy - Location When In Use Usage Description
。
為此,只需前往 info.plist 並單擊+ 按鈕並粘貼Privacy - Location When In Use Usage Description
。
不要忘記添加評論,讓用戶了解您為什么要求他們提供 position。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.