swift photo album camera permissions processing example details
- 2020-05-30 21:10:28
- OfStack
preface
Recently, I met the demand of obtaining the permission of photo album camera in the development process. I finally solved the problem by searching the relevant information, and I decided to record it because I would use it later. I won't say any more, let's have a look at the detailed introduction.
Note: must be in first
info.plist
Add two keyNSPhotoLibraryUsageDescription, and NSCameraUsageDescription, and fill in the prompt content.
The camera
// use
self.cameraPermissions(authorizedBlock: {
print(" Open the camera ")
}, deniedBlock: {
print(" No permission to use the camera ")
})
// Camera permissions
class func cameraPermissions(authorizedBlock: OperationBlock?, deniedBlock: OperationBlock?) {
let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
// .notDetermined .authorized .restricted .denied
if authStatus == .notDetermined {
// The first 1 Secondary trigger authorization 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!()
}
}
}
Photo album
iOS 9.0 ALAssetsLibrary is out of date, use PHPhotoLibrary instead
// use
self.photoAlbumPermissions(authorizedBlock: {
print(" Open the photo album ")
}, deniedBlock: {
print(" No permission to open the album ")
})
// Photo album permissions
class func photoAlbumPermissions(authorizedBlock: OperationBlock?, deniedBlock: OperationBlock?) {
let authStatus = PHPhotoLibrary.authorizationStatus()
// .notDetermined .authorized .restricted .denied
if authStatus == .notDetermined {
// The first 1 Secondary trigger authorization 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!()
}
}
}
conclusion