Permission Lớp lót là một cách hiệu quả để tránh một tình huống mà khách hàng của bạn có thể từ chối truy cập đến một tính năng quan trọng ứng dụng của bạn.
Trên iOS, ứng dụng chỉ được phép kích hoạt quyền hệ thống mặc định một lần cho mỗi đối tượng địa lý. Cho phép mồi là khi một ứng dụng "số nguyên tố" Khách hàng có cảnh báo bắt chước quyền của hệ thống.
Lợi ích khi thực hiện điều này là nếu Khách hàng chọn không tham gia (chọn Hủy), Ứng dụng vẫn có thể hỏi lại trong tương lai, cho đến khi họ nói có - tại thời điểm đó quyền hệ thống thực tế được hiển thị và Khách hàng ít có khả năng thống kê sau đó thay đổi suy nghĩ của mình và tham gia vào luồng công việc tiêu cực.
Hơn nữa, kể từ khi cameraSelected()
Thực hiện công việc này, nếu giảm sử dụng, nhưng sau đó tại một số điểm trong tương lai không thay đổi cài đặt của họ, App sẽ ngay lập tức phản ánh các điều khoản mới mà không cần nhập thêm (ví dụ. User có thể chuyển sang Cài đặt , thay đổi quyền và sau đó chuyển về Ứng dụng).
Dưới đây là một số Swift 3 mã để thực hiện tính năng này:
[CẬP NHẬT: Đã bao gồm là một giải pháp để mở một liên kết sâu vào Settings nơi người dùng có thể cho phép truy cập camera, nếu họ đã bị từ chối trước đây nó]
[UPDATE 2:. thêm dòng mẫu cho triển khai Analytics]
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)
}
.