[英]How to set a prompt for second time when user has clicked Don't allow for permissions the first time?
[英]How to detect user has clicked Don't Allow access to camera
我正在使用UIImagePicker
向用戶展示相機以拍攝將在應用中使用的照片。
我的問題是,當用戶第一次打開圖像選擇器時,他們會看到一個提示:'“我的應用程序”想要使用兩個選項訪問您的相機,不允許和確定。
我的要求是,當用戶單擊“不允許”時,圖像選擇器將被關閉,留下黑色視圖。 有沒有辦法檢測用戶選擇了不允許?
這是我提供UIImagePicker
代碼:
var PhotoPicker:UIImagePickerController = UIImagePickerController()
PhotoPicker.delegate = self
PhotoPicker.sourceType = .Camera
PhotoPicker.cameraFlashMode = .Off
PhotoPicker.showsCameraControls = false
PhotoPicker.cameraDevice = .Rear
self.presentViewController(PhotoPicker, animated: false, completion: nil)
要檢測對庫的訪問:
您需要使用AssetsLibrary。 一,導入資產庫框架:
import AssetsLibrary
然后,請求授權狀態,如果未確定,則使用塊來捕獲這些事件,如下所示:
if ALAssetsLibrary.authorizationStatus() == ALAuthorizationStatus.NotDetermined {
let library = ALAssetsLibrary()
library.enumerateGroupsWithTypes(.All, usingBlock: { (group, stop) -> Void in
// User clicked ok
}, failureBlock: { (error) -> Void in
// User clicked don't allow
imagePickerController.dismissViewControllerAnimated(true, completion: nil)
})
}
要檢測對相機的訪問:
你需要使用AVFoundation。 一,導入avfoundation框架:
import AVFoundation
然后,如前所述,當您轉到imagepicker並捕獲事件時請求用戶權限。
if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) == AVAuthorizationStatus.NotDetermined {
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in
// User clicked ok
if (videoGranted) {
// User clicked don't allow
} else {
imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}
})
}
希望能幫助到你!
在iOS 10中,使用:
import Photos
let authStatus = PHPhotoLibrary.authorizationStatus()
if authStatus == .notDetermined || authStatus == .denied {
PHPhotoLibrary.requestAuthorization({ (status) in
if status == PHAuthorizationStatus.authorized {
} else {
imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}
})
}
看看這個用於檢測相機許可
用戶選擇“不允許”時使用此選項。
PhotoPicker.dismissViewControllerAnimated(false, completion: nil)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.