[英]Get notified when the user makes selection for allowing access to Camera in iOS
當應用程序嘗試訪問iOS中的Camera API時,會顯示操作系統級別的警報視圖。 此處的用戶必須允許訪問攝像頭或禁用訪問。
我的問題是如何才能收到用戶選擇的通知..?
假設他選擇了不允許訪問,而不是我可以在我的應用程序中使用任何通知...?
任何幫助表示贊賞。
當相機出現時,您可以檢查當前的授權狀態,並手動請求授權,而不是讓操作系統顯示警報視圖。 這樣,當用戶接受/拒絕您的請求時,您會收到回調。
在快速:
let status = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
if status == AVAuthorizationStatus.Authorized {
// Show camera
} else if status == AVAuthorizationStatus.NotDetermined {
// Request permission
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted) -> Void in
if granted {
// Show camera
}
})
} else {
// User rejected permission. Ask user to switch it on in the Settings app manually
}
如果用戶先前已拒絕該請求,則調用requestAccessForMediaType
將不會顯示警報並將立即執行完成塊。 在這種情況下,您可以選擇顯示自定義提醒並將用戶鏈接到設置頁面。 在此更多信息在這里 。
從Kens的回答中,我創建了這個Swift 3協議來處理權限訪問:
import AVFoundation
protocol PermissionHandler {
func handleCameraPermissions(completion: @escaping ((_ error: Error?) -> Void))
}
extension PermissionHandler {
func handleCameraPermissions(completion: @escaping ((_ error: Error?) -> Void)) {
let status = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
switch status {
case .authorized:
completion(nil)
case .restricted:
completion(ClientError.noAccess)
case .notDetermined:
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { granted in
if granted {
completion(nil)
} else {
completion(ClientError.noAccess)
}
}
case .denied:
completion(ClientError.noAccess)
}
}
}
然后,您可以遵循此協議並在您的類中調用它,如下所示:
handleCameraPermissions() { error in
if let error = error {
//Denied, handle error here
return
}
//Allowed! As you were
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.