2016-03-15 100 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

回答