升級 iOS 10 之後目測坑還是挺多的,記錄一下吧,看看到時候會不會成爲一個系列。
直入正題吧
今天在寫 Swift 3 相關的一個項目小小練下手,發現調用相機,崩了。試試看調用相冊,又特麼崩了。然後看到控制檯輸出了以下信息:This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.
意思就是說,你需要在 info.plist 文件添加一個 NSPhotoLibraryUsageDescription
的 key,然後添加一個描述。
解決方案
1.在項目中找到info.plist
文件,右鍵點擊以 Source Code
形式打開
2.添加以下鍵值對,這裏以 PhotoLibrary 作爲例子
<key>NSPhotoLibraryUsageDescription</key>
<string>此 App 需要您的同意才能讀取媒體資料庫</string>
注意,key 是絕對不能寫錯的,
而 value 也就是<string></string>你可以隨意寫
附圖
目測所有需要的權限描述都會在 iOS 10 beta裏面crash,不用怕,控制檯會有輸出的,自行添加就 OK,我另外附送幾個 key/value 給你們
<key>NSCameraUsageDescription</key>
<string>cameraDesciption</string>
<key>NSContactsUsageDescription</key>
<string>contactsDesciption</string>
<key>NSMicrophoneUsageDescription</key>
<string>microphoneDesciption</string>
嗯,然後 clean 一下,重新運行。
當然我比較推薦的做法是直接info.plist
還是用 Property List
的形式,然後點擊添加, 就是滑動到最後一個 item 會出現一個小加號
輸入 Privacy 可以迅速定位到這一權限系列,找到你需要的權限,修改後面的 value 就可以了
原文鏈接:http://www.jianshu.com/p/c212cde86877
present進入相機的時候,有個回調方法, [self presentViewController:picker animated:YES completion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[picker viewDidLayoutSubviews];
});
}];
相機:let authStatus:AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
if (authStatus == .denied || authStatus == .restricted){
//沒有權限
}else{
//有權限
}
相冊:let library:PHAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
if(library == .denied || library == .restricted){
//沒有
}else{
//有
}
跳轉設置頁面:UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)