如果用户允许使用 UIImagePicker 访问照片库,如何签入 Swift?
How to check in Swift if the user gave permission to access the photolibrary with UIImagePicker?
我目前正在开发一款应用程序,可以让用户预览他用相机拍摄的最后一张图片,但我不知道如何检查用户是否授予访问图库中所有图片的权限, 选定的图片或他拒绝访问。
有一种方法或 class 我可以导入以检查用户授予我的应用程序的权限类型吗?
您可以使用以下方法查看图库的权限状态。
如果权限被拒绝,它也会将您导航到应用程序设置屏幕。
func checkGalleryPermission()
{
let authStatus = PHPhotoLibrary.authorizationStatus()
switch authStatus
{
case .denied : print("denied status")
let alert = UIAlertController(title: "Error", message: "Photo library status is denied", preferredStyle: .alert)
let cancelaction = UIAlertAction(title: "Cancel", style: .default)
let settingaction = UIAlertAction(title: "Setting", style: UIAlertAction.Style.default) { UIAlertAction in
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: { _ in })
}
}
alert.addAction(cancelaction)
alert.addAction(settingaction)
Viewcontoller.present(alert, animated: true, completion: nil)
break
case .authorized : print("success")
//open gallery
break
case .restricted : print("user dont allowed")
break
case .notDetermined : PHPhotoLibrary.requestAuthorization({ (newStatus) in
if (newStatus == PHAuthorizationStatus.authorized) {
print("permission granted")
//open gallery
}
else {
print("permission not granted")
}
})
break
case .limited:
print("limited")
@unknown default:
break
}
}
我目前正在开发一款应用程序,可以让用户预览他用相机拍摄的最后一张图片,但我不知道如何检查用户是否授予访问图库中所有图片的权限, 选定的图片或他拒绝访问。
有一种方法或 class 我可以导入以检查用户授予我的应用程序的权限类型吗?
您可以使用以下方法查看图库的权限状态。 如果权限被拒绝,它也会将您导航到应用程序设置屏幕。
func checkGalleryPermission()
{
let authStatus = PHPhotoLibrary.authorizationStatus()
switch authStatus
{
case .denied : print("denied status")
let alert = UIAlertController(title: "Error", message: "Photo library status is denied", preferredStyle: .alert)
let cancelaction = UIAlertAction(title: "Cancel", style: .default)
let settingaction = UIAlertAction(title: "Setting", style: UIAlertAction.Style.default) { UIAlertAction in
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: { _ in })
}
}
alert.addAction(cancelaction)
alert.addAction(settingaction)
Viewcontoller.present(alert, animated: true, completion: nil)
break
case .authorized : print("success")
//open gallery
break
case .restricted : print("user dont allowed")
break
case .notDetermined : PHPhotoLibrary.requestAuthorization({ (newStatus) in
if (newStatus == PHAuthorizationStatus.authorized) {
print("permission granted")
//open gallery
}
else {
print("permission not granted")
}
})
break
case .limited:
print("limited")
@unknown default:
break
}
}