用到了記錄下.
須先在info.plist
中添加兩個(gè)keyNSPhotoLibraryUsageDescription
, 和NSCameraUsageDescription
, 內(nèi)容填提示內(nèi)容.
相機(jī)
// 使用
self.cameraPermissions(authorizedBlock: {
print("打開(kāi)相機(jī)")
}, deniedBlock: {
print("沒(méi)有權(quán)限使用相機(jī)")
})
// 相機(jī)權(quán)限
class func cameraPermissions(authorizedBlock: OperationBlock?, deniedBlock: OperationBlock?) {
let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
// .notDetermined .authorized .restricted .denied
if authStatus == .notDetermined {
// 第一次觸發(fā)授權(quán) 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!()
}
}
}
相冊(cè)
iOS9.0后ALAssetsLibrary
過(guò)時(shí)了, 換成使用PHPhotoLibrary
// 使用
self.photoAlbumPermissions(authorizedBlock: {
print("打開(kāi)相冊(cè)")
}, deniedBlock: {
print("沒(méi)有權(quán)限打開(kāi)相冊(cè)")
})
// 相冊(cè)權(quán)限
class func photoAlbumPermissions(authorizedBlock: OperationBlock?, deniedBlock: OperationBlock?) {
let authStatus = PHPhotoLibrary.authorizationStatus()
// .notDetermined .authorized .restricted .denied
if authStatus == .notDetermined {
// 第一次觸發(fā)授權(quán) 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!()
}
}
}