1

我的观点是:

  • UIApplicationDidBecomeActiveNotification使用调用选择器创建观察者
  • 依次向用户请求以下权限:使用相机、位置和接收推送通知。
  • 该视图具有三个UIButton状态,具体取决于每个权限状态,如果任何权限被拒绝,则将用户导航到设置
  • 点击代表具有拒绝状态的权限的按钮将用户导航到设置
  • 隐藏每个警报后,使用观察者操作,触发下一个警报并更新所有按钮状态以反映任何更改

授予所有权限后,它会推送下一个视图以及其余的注册/输入流程。

问题是:在某些设备上,当从干净状态(删除并重新安装应用程序)运行应用程序时,位置和通知的权限默认设置为拒绝,就像向用户显示被拒绝的警报一样。

我无法确定这背后的任何合理问题,除了一些过时版本的剩余设置在安装新版本时不会被删除。这个视图似乎是唯一可能触发这些警报的地方。

有没有人有类似的问题并且可以提出任何建议?

4

1 回答 1

4

我建议您在要求用户使用它之前尝试检查位置服务和通知服务的状态。因为如果用户要在您请求他许可的那一刻禁用这些,他将需要转到设置并在那里启用它。您应该尝试检测用户是否禁用了位置/通知/相机。

用于相机:

func accessToCamera(granted: @escaping (() -> Void)) {
    if UIImagePickerController.isSourceTypeAvailable(.camera) {

        let status = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeAudio)

        if status == .authorized {
            granted()
        } else if status == .denied {
            self.cameraPermissionAlert()
        } else if status == .notDetermined {

            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (accessAllowed) in
                if accessAllowed {
                    granted()
                } else {
                    self.cameraPermissionAlert()
                }
            })
        } else if status == .restricted {
            self.cameraPermissionAlert()
        }
    } else {
       print("Camera not available on this device")
    }
}

func cameraPermissionAlert() {
    let alert = UIAlertController(title: "Access to camera not available", message: "Please enable access to camera in order to use this feature", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: { (action) in
        if let url = URL(string: UIApplicationOpenSettingsURLString) {
            if UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        }
    }))

    alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
    if let top = UIApplication.topViewController() { // This is extension to UIApplication that finds top view controller and displays it
        top.present(alert, animated: true, completion: nil)
        }
    }

对于远程通知,您可以使用以下内容: Determin on iPhone if user has enabled push notifications

对于定位服务: 检查是否启用了定位服务

在这两种情况下,您都可以检测用户是否禁用了此功能,并向用户提供具有打开设置功能的警报控制器。

于 2017-03-30T08:59:05.600 回答