swift 判断相册权限
import Photos
if #available(iOS 11.0, *) {
PHPhotoLibrary.requestAuthorization { (status) in
if status == PHAuthorizationStatus.authorized || status == PHAuthorizationStatus.notDetermined {
//打开相册的操作
self.openAlbum()
}else{
//去设置
self.openSystemSettingPhotoLibrary()
}
}
}else{
if self.canPhotoLibary() {
self.openAlbum()
}else{
self.openSystemSettingPhotoLibrary()
}
}
//iOS11 以前这样判断
func canPhotoLibary() ->Bool{
let authStatus : PHAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
if authStatus == PHAuthorizationStatus.authorized || authStatus == PHAuthorizationStatus.notDetermined { //不确定与已授权
return true
}else{//限制与未授权
return false
}
}
//弹出弹窗去设置
func openSystemSettingPhotoLibrary() {
let alert = UIAlertController(title:"未获得权限访问您的照片", message:"请在设置选项中允许720yun访问您的照片", preferredStyle: .alert)
let confirm = UIAlertAction(title:"去设置", style: .default) { (_)in
let url=URL.init(string: UIApplicationOpenSettingsURLString)
if UIApplication.shared.canOpenURL(url!){
UIApplication.shared.open(url!, options: [:], completionHandler: { (ist)in
})
}
}
let cancel = UIAlertAction(title:"取消", style: .cancel, handler:nil)
alert.addAction(cancel)
alert.addAction(confirm)
self.present(alert, animated:true, completion:nil)
}