【发布时间】:2016-06-30 07:13:28
【问题描述】:
我正在尝试控制需要访问设备摄像头的应用程序的流程。这个想法是检查摄像头访问设置,如果不允许摄像头访问,让用户有机会直接进入应用程序的设置。然后,一旦更改(或不更改)相机开关,就将用户返回到应用程序。
当应用程序从设备运行时,以下代码似乎会执行此操作。但是,如果设备被绑定并且应用程序从 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
【问题讨论】:
-
好吧。对你来说可能是重复的,但对我来说不是。如前所述,随着应用程序在设备上运行,代码可以正常工作。诚然,我只测试了几十次,但在设备上,该应用程序没有一次崩溃。事实上,Apple 会自动在左上角添加一个链接,以将用户返回到应用程序。该链接确实将用户返回到继续运行的应用程序。我只有在连接并从 Xcode 开始时才遇到过这个问题。也许最终结果是一样的,但如果苹果添加了返回应用的链接,他们肯定有一个预期,它会起作用。
标签: ios xcode application-settings