【问题标题】:Checking permissions in Swift 2.0在 Swift 2.0 中检查权限
【发布时间】:2017-02-12 09:52:20
【问题描述】:

无论我在哪里添加代码来检查相机/麦克风/照片之类的权限,弹出式确认总是会终止我的应用或将我返回几个视图控制器。

一个例子如下。

当我有一个处理权限的页面时,我几乎没有视图控制器(通过注册过程的一部分)。用户点击一个按钮来处理使用以下代码的相机权限。

if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) !=  AVAuthorizationStatus.Authorized {
        AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted :Bool) -> Void in
            if granted == true {
                // do something
            } else {
               // determine whether not determined, denied etc and do something else
            }
        });
    }

但是,一旦弹出 iOS 确认信息,它就会将应用程序返回 2 个视图控制器。在其他情况下(例如在 viewDidLoad 上)权限请求会在做出选择后立即终止应用。

任何想法我在我的设置中缺少什么或如何防止这种行为?

谢谢。

【问题讨论】:

  • 拒绝怎么样?
  • @Misha - 我知道有更多选项供用户选择,如果需要,这些选项将被处理,但与我认为的问题无关
  • 但是如果权限被拒绝,应用程序将如何决定该怎么做?在另一个说明中,您是否在 plist 中添加了密钥?
  • @misha - 它会根据代码所说的来决定做什么 - 在这种情况下,它实际上并没有做任何事情,但关键是当弹出窗口显示时视图会回滚 -其余的无关紧要,但我会编辑问题以避免未来的混乱
  • 不管它在未经授权的情况下做什么,关键是我总是授权,但进程崩溃或回滚视图,无论是在它要求做出决定时还是在授权时

标签: ios permissions swift2 touch-up-inside


【解决方案1】:

我想你误解了我的评论,我的意思是

if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) !=  AVAuthorizationStatus.Authorized { // here you are checking if it's not authorized i..e  it's denied, NotDetermined or Restricted
   ....
    }
 else if if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) ==  AVAuthorizationStatus.Authorized
// do the something in case it's autorized

我在这里列出键 -

     <key>NSPhotoLibraryUsageDescription</key>
     <string>This app requires access to the photo library.</string>
     <key>NSCameraUsageDescription</key>
     <string>This app requires access to the camera.</string>

【讨论】:

  • 谢谢 Misha - 我有状态检查来确定当前的状态和其他代码(granted==true)来确定他们在弹出窗口中的选择 - 至于我添加这些的键因为我有一个用于 NSLocationWhenInUseUsageDescription 但相机/照片的关键是“隐私 - 相机使用说明”(不确定我从哪里得到该密钥) - 将很快回来查看结果
  • 似乎我在 info.plist 中缺少一个条目,其中一个描述导致它弹出到最近的 navigationController(这很奇怪,但没关系) - 添加我能想到的所有描述of 似乎已经解决了问题
猜你喜欢
  • 2016-01-08
  • 2019-10-31
  • 2021-07-25
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 2016-07-22
相关资源
最近更新 更多