在確保最佳體驗的同時提示用戶訪問攝像頭(或其他功能)的最有效方法是什麼?在iOS中請求攝像頭權限對話框啓動(Prime權限)
訪問相機時,iOS必須要求客戶允許訪問。衆所周知,如果客戶說「不」,但隨後改變主意,則無法在應用程序中反轉這一決定。他們必須去設置和遵循一些步驟來重新啓用訪問,即:
Settings -> Privacy -> Camera -> [Your App] -> turn switch on
在確保最佳體驗的同時提示用戶訪問攝像頭(或其他功能)的最有效方法是什麼?在iOS中請求攝像頭權限對話框啓動(Prime權限)
訪問相機時,iOS必須要求客戶允許訪問。衆所周知,如果客戶說「不」,但隨後改變主意,則無法在應用程序中反轉這一決定。他們必須去設置和遵循一些步驟來重新啓用訪問,即:
Settings -> Privacy -> Camera -> [Your App] -> turn switch on
權限底漆是爲了避免一種情況,你的客戶可能會拒絕一個關鍵功能接入的有效途徑的應用程序。
在iOS上,只允許每個功能觸發默認系統權限一次。權限啓動是指應用程序通過模仿系統權限的警報「啓動」客戶。
這樣做的好處是,如果客戶選擇退出(選擇取消),該應用程序仍然可以在將來再次詢問,直到他們說是 - 在此時顯示實際的系統權限以及客戶在統計上不太可能改變主意並進入負面的工作流程。
此外,由於cameraSelected()
執行該流程,如果用戶拒絕,但後來在未來的某個時刻確實更改它們的設置,應用程序將立即反映沒有進一步輸入新的權限(即用戶可以切換到設置,更改權限,然後切換回應用程序)。
下面是一些斯威夫特3代碼來實現此功能:
[更新:包括是打開一個深層鏈接設置,用戶可以啓用攝像頭的訪問,如果他們之前都否認的解決方案它]
[UPDATE 2:添加樣品分析執行線]
func cameraSelected() {
// First we check if the device has a camera (otherwise will crash in Simulator - also, some iPod touch models do not have a camera).
if let deviceHasCamera = UIImagePickerController.isSourceTypeAvailable(.camera) {
let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
switch authStatus {
case .authorized:
showCameraPicker()
case .denied:
alertPromptToAllowCameraAccessViaSettings()
case .notDetermined:
permissionPrimeCameraAccess()
default:
permissionPrimeCameraAccess()
}
} else {
let alertController = UIAlertController(title: "Error", message: "Device has no camera", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { (alert) in
Analytics.track(event: .permissionsPrimeCameraNoCamera)
})
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
}
func alertPromptToAllowCameraAccessViaSettings() {
let alert = UIAlertController(title: "\"<Your App>\" Would Like To Access the Camera", message: "Please grant permission to use the Camera so that you can <customer benefit>.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Open Settings", style: .cancel) { alert in
Analytics.track(event: .permissionsPrimeCameraOpenSettings)
if let appSettingsURL = NSURL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.openURL(appSettingsURL)
}
})
present(alert, animated: true, completion: nil)
}
func permissionPrimeCameraAccess() {
let alert = UIAlertController(title: "\"<Your App>\" Would Like To Access the Camera", message: "<Your App> would like to access your Camera so that you can <customer benefit>.", preferredStyle: .alert)
let allowAction = UIAlertAction(title: "Allow", style: .default, handler: { (alert) -> Void in
Analytics.track(event: .permissionsPrimeCameraAccepted)
if AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count > 0 {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { [weak self] granted in
DispatchQueue.main.async {
self?.cameraTabSelected() // try again
}
})
}
})
alert.addAction(allowAction)
let declineAction = UIAlertAction(title: "Not Now", style: .cancel) { (alert) in
Analytics.track(event: .permissionsPrimeCameraCancelled)
}
alert.addAction(declineAction)
present(alert, animated: true, completion: nil)
}
func showCameraPicker() {
let picker = UIImagePickerController()
picker.delegate = self
picker.modalPresentationStyle = UIModalPresentationStyle.currentContext
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
present(picker, animated: true, completion: nil)
}
。