iOS 10 开发适配系列 之 权限Crash问题

来源:互联网 发布:男科网络咨询说话技巧 编辑:程序博客网 时间:2024/04/19 15:36

升级 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>你可以随意写

附图


图片发自简书App

目测所有需要的权限描述都会在 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 就可以了



文/wzbdroid(简书作者)
原文链接:http://www.jianshu.com/p/c212cde86877
present进入相机的时候,有个回调方法, [self presentViewController:picker animated:YES completion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[picker viewDidLayoutSubviews];
});
}]; 
相册import Photo,相机import AVFoundation.
相机: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)!)

0 0
原创粉丝点击