前言
最近在开发中遇到了获取相册相机权限的需求,通过查找相关的资料终于解决了,因为之后会用到所以决定记录下,下面话不多说了,来一起看看详细的介绍吧。
注意:须先在info.plist
中添加两个keyNSPhotoLibraryUsageDescription, 和NSCameraUsageDescription, 内容填提示内容.
相机
// 使用self.cameraPermissions(authorizedBlock: { print("打开相机")}, deniedBlock: { print("没有权限使用相机")})// 相机权限class func cameraPermissions(authorizedBlock: OperationBlock?, deniedBlock: OperationBlock?) { let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video) // .notDetermined .authorized .restricted .denied if authStatus == .notDetermined { // 第一次触发授权 alert AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in self.cameraPermissions(authorizedBlock: authorizedBlock, deniedBlock: deniedBlock) }) } else if authStatus == .authorized { if authorizedBlock != nil { authorizedBlock!() } } else { if deniedBlock != nil { deniedBlock!() } }}
相册
iOS9.0后ALAssetsLibrary过时了, 换成使用PHPhotoLibrary
// 使用self.photoAlbumPermissions(authorizedBlock: { print("打开相册")}, deniedBlock: { print("没有权限打开相册")})// 相册权限class func photoAlbumPermissions(authorizedBlock: OperationBlock?, deniedBlock: OperationBlock?) { let authStatus = PHPhotoLibrary.authorizationStatus() // .notDetermined .authorized .restricted .denied if authStatus == .notDetermined { // 第一次触发授权 alert PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) -> Void in self.photoAlbumPermissions(authorizedBlock: authorizedBlock, deniedBlock: deniedBlock) } } else if authStatus == .authorized { if authorizedBlock != nil { authorizedBlock!() } } else { if deniedBlock != nil { deniedBlock!() } }}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对VEVB武林网的支持。
新闻热点
疑难解答