2016-10-04 117 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