import Photos
if #available(iOS 11.0, *) {
? ? ? ? ? ? PHPhotoLibrary.requestAuthorization { (status) in
? ? ? ? ? ? ? ? if status == PHAuthorizationStatus.authorized || status == PHAuthorizationStatus.notDetermined?{
? ? ? ? ? ? ? ? ? ? //打開相冊的操作
? ? ? ? ? ? ? ? ? ? self.openAlbum()
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? //去設(shè)置
? ? ? ? ? ? ? ? ? ? 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 { //不確定與已授權(quán)
? ? ? ? ? ? return true
? ? ? ? }else{//限制與未授權(quán)
? ? ? ? ? ? return false
? ? ? ? }
? ? }
//彈出彈窗去設(shè)置
func openSystemSettingPhotoLibrary() {
? ? ? ? let alert = UIAlertController(title:"未獲得權(quán)限訪問您的照片", message:"請在設(shè)置選項中允許720yun訪問您的照片", preferredStyle: .alert)
? ? ? ? let confirm = UIAlertAction(title:"去設(shè)置", 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)
? ? }