Skip to content

Instantly share code, notes, and snippets.

@quangtqag
Last active July 1, 2016 08:12
Show Gist options
  • Save quangtqag/609957df41a78068590df6ce3502018c to your computer and use it in GitHub Desktop.
Save quangtqag/609957df41a78068590df6ce3502018c to your computer and use it in GitHub Desktop.
import AVFoundation
private func showDialogWithTitle(title: String?, msg: String?) {
let alertController = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
@IBAction func didTapButton(sender: AnyObject) {
let authorizationStatus: AVAuthorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
if authorizationStatus == AVAuthorizationStatus.NotDetermined {
let alertController = UIAlertController(title: nil, message: "Please tap OK button in next dialog to allow the app access camera for taking identity photos.", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { action in
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted: Bool) in
if granted == true {
// User granted
dispatch_async(dispatch_get_main_queue(), {
self.showDialogWithTitle(nil, msg: "User granted")
})
}
else {
// User rejected
dispatch_async(dispatch_get_main_queue(), {
self.showDialogWithTitle(nil, msg: "User rejected")
})
}
})
}))
self.presentViewController(alertController, animated: true, completion: nil)
}
else if authorizationStatus == AVAuthorizationStatus.Authorized {
self.showDialogWithTitle(nil, msg: "User authoried")
}
else if authorizationStatus == AVAuthorizationStatus.Denied {
let alertController = UIAlertController(
title: nil,
message: RS_GOTO_SETTINGS_ALLOW_ACCESS_CAMERA,
preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: RS_OK, style: UIAlertActionStyle.Cancel, handler: nil))
alertController.addAction(UIAlertAction(
title: RS_GOTO_SETTINGS_APP,
style: UIAlertActionStyle.Default,
handler: { (action) in
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment