2016-03-15 157 views
0

我正在嘗試控制需要訪問設備攝像頭的應用程序的流程。這個想法是檢查攝像頭訪問設置,如果不允許攝像頭訪問,請讓用戶有機會直接進入該應用的設置。然後,一旦相機開關改變(或不改變),將用戶返回到應用程序。應用程序到設備設置,返回到應用程序休息Xcode swift

下面的代碼似乎這樣做,當應用程序從設備運行。但是,如果設備被連接並且應用程序從Xcode啓動,那麼觸摸開關的瞬間,應用程序就會崩潰。沒有信息寫入控制檯 - 只是AppDelegate第一行的可怕突出部分。很明顯,我不相信它實際上在設備上「工作」。

任何幫助,將不勝感激。當用戶調整有關它的系統設置

的Xcode 7.2.1 IOS 9.2.1

var userOkForCamera : Bool = false 


@IBAction func takeInvItemPhoto(sender: UIButton) { 

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) { 

     if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) == AVAuthorizationStatus.Authorized { 
      // Already Authorized 
      // This seems to work ok when the use auth switch is On 

      userOkForCamera = true 

      let picker = UIImagePickerController() 
      picker.delegate = self 
      picker.sourceType = UIImagePickerControllerSourceType.Camera 
      picker.mediaTypes = [kUTTypeImage as String] 
      picker.allowsEditing = false 
      presentViewController(picker, animated: true, completion: nil) 

     } else { 
      userOkForCamera = false 
     }//if auth status else 

     if userOkForCamera == false { 
      showCameraAcessDeniedAlert() 
      return 
     }// if user ok false 

    } else {//if camera 

     let ac = UIAlertController(title: "Source Not Available", message: "The camera is not available.", preferredStyle: .Alert) 
       ac.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil)) 
       presentViewController(ac, animated: true, completion: nil) 

    }//if camera else 

}//takeInvItemPhoto 

    func showCameraAcessDeniedAlert() { 
    let alertController = UIAlertController(title: "Uh-ooh!", 
     message: "It looks like camera permission is not authorized. Please enable it in Settings to continue.", 
     preferredStyle: .Alert) 

    let settingsAction = UIAlertAction(title: "Settings", style: .Default) { (alertAction) in 

     if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) { 
      UIApplication.sharedApplication().openURL(appSettings) 
     }//if let 
    }//settings action block 

    alertController.addAction(settingsAction) 

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 
    alertController.addAction(cancelAction) 

    presentViewController(alertController, animated: true, completion: nil) 

}//showCameraAcessDeniedAlert 

enter image description here

+0

好吧。它可能是你的重複,但不適合我。如上所述,在設備上運行應用程序時,代碼正常工作。當然,我只測試了幾十次,但在設備上應用程序沒有一次崩潰。實際上,Apple會在左上角自動添加一個鏈接,以將用戶返回到應用程序。該鏈接確實將用戶返回到繼續運行的應用程序。從Xcode開始,我只會遇到這個問題。也許最終的結果是一樣的,但如果蘋果增加了鏈接返回到應用程序,他們必須有一個預期,它會工作。 – user2698617

回答

相關問題