當應用程序嘗試訪問iOS中的相機API時,顯示的操作系統級別爲alertview。 用戶在這裏必須允許訪問攝像機或禁用訪問。當用戶選擇允許訪問iOS中的相機時收到通知
我的問題是如何得到用戶所做選擇的通知..?
說他選擇不允許訪問比在那裏提出的任何通知,我可以用在我的應用程序..?
任何幫助表示讚賞。
當應用程序嘗試訪問iOS中的相機API時,顯示的操作系統級別爲alertview。 用戶在這裏必須允許訪問攝像機或禁用訪問。當用戶選擇允許訪問iOS中的相機時收到通知
我的問題是如何得到用戶所做選擇的通知..?
說他選擇不允許訪問比在那裏提出的任何通知,我可以用在我的應用程序..?
任何幫助表示讚賞。
您可以檢查當前授權狀態並手動請求授權,而不是讓操作系統在相機出現時顯示警報視圖。這樣,當用戶接受/拒絕您的請求時,您會得到一個回調。
在SWIFT:
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
將不會顯示警覺,並會立即執行完畢塊。在這種情況下,您可以選擇顯示自定義警報並將用戶鏈接到設置頁面。更多關於這個here的信息。
從KENS答案兩者,我創建了這個斯威夫特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
謝謝,作品完美! 不要忘記導入AVFoundation。 – 2015-05-13 17:30:56