2016-10-04 89 views
2

无论我在哪里添加代码来检查相机/麦克风/照片等权限,弹出确认总是会杀死我的应用程序或将我发回给几个视图控制器。在Swift 2.0中检查权限

一个例子如下。

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

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)权限请求一旦做出选择,就会终止应用程序。

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

感谢。

+0

怎么拒绝? – Misha

+0

@Misha - 据我所知,还有更多的选项供用户选择,如果需要,这些选项会被处理,但与我认为 – RobertyBob

+0

的问题无关,但是如果拒绝许可,应用程序将如何决定如何处理?在plist中添加键? – Misha

回答

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> 
+0

谢谢米沙 - 我有状态检查以确定状态是什么,其他代码(授予== true)用于确定他们在弹出窗口上的选择 - 至于我添加这些键,因为我有一个NSLocationWhenInUseUsageDescription,但相机/照片的关键'隐私 - 相机使用说明'(不知道我从哪里得到该键) - 将很快检查结果 – RobertyBob

+0

似乎我有一个在info.plist中缺少一个描述的条目,导致它弹出到最近的navigationController(这很奇怪,但不管) - 添加了所有我能想到的描述似乎已经排序了这个问题 – RobertyBob