Home:ALL Converter>Presenting camera permission dialog in iOS 8

Presenting camera permission dialog in iOS 8

Ask Time:2014-09-12T15:43:01         Author:jamix

Json Formatter

When my app tries to access the camera for the first time on iOS 8, the user is presented with a camera permission dialog, much like the microphone one for microphone access in iOS 7.

In iOS 7, it was possible to invoke the microphone permission dialog beforehand and see if the permission was granted (see this question, for example). Is there a similar way to invoke the camera permission dialog in iOS 8? Can the dialog be combined for microphone AND camera access permission?

Author:jamix,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25803217/presenting-camera-permission-dialog-in-ios-8
Crashalot :

None of the answers seem to check for both microphone and camera permissions. Our code checks against the scenario where camera permissions are granted but microphone access is denied.\n\nSince we're new to Swift, it's unlikely the gnarly nested closures and if statements are optimal. Please share suggestions for improving the code! But at least it works so far in testing.\n\n AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in\n if (videoGranted) {\n AVCaptureDevice.requestAccessForMediaType(AVMediaTypeAudio, completionHandler: { (audioGranted: Bool) -> Void in\n if (audioGranted) {\n dispatch_async(dispatch_get_main_queue()) {\n // Both video & audio granted\n }\n } else {\n // Rejected audio\n }\n })\n } else {\n // Rejected video\n }\n })\n",
2015-07-07T08:28:36
Sourabh Sharma :

\nSwift 3.0 Solution\n\nimport AVFoundation\n\n\nNote: add Privacy - Camera Usage Description key on your Info.plist\n\n//MARK: Camera Handling\n\n func callCamera(){\n let myPickerController = UIImagePickerController()\n myPickerController.delegate = self;\n myPickerController.sourceType = UIImagePickerControllerSourceType.camera\n\n self.present(myPickerController, animated: true, completion: nil)\n NSLog(\"Camera\");\n }\n func checkCamera() {\n let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)\n switch authStatus {\n case .authorized: callCamera() // Do your stuff here i.e. callCameraMethod()\n case .denied: alertToEncourageCameraAccessInitially()\n case .notDetermined: alertPromptToAllowCameraAccessViaSetting()\n default: alertToEncourageCameraAccessInitially()\n }\n }\n\n func alertToEncourageCameraAccessInitially() {\n let alert = UIAlertController(\n title: \"IMPORTANT\",\n message: \"Camera access required for capturing photos!\",\n preferredStyle: UIAlertControllerStyle.alert\n )\n alert.addAction(UIAlertAction(title: \"Cancel\", style: .default, handler: nil))\n alert.addAction(UIAlertAction(title: \"Allow Camera\", style: .cancel, handler: { (alert) -> Void in\n UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)\n }))\n present(alert, animated: true, completion: nil)\n }\n\n func alertPromptToAllowCameraAccessViaSetting() {\n\n let alert = UIAlertController(\n title: \"IMPORTANT\",\n message: \"Camera access required for capturing photos!\",\n preferredStyle: UIAlertControllerStyle.alert\n )\n alert.addAction(UIAlertAction(title: \"Dismiss\", style: .cancel) { alert in\n if AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count > 0 {\n AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { granted in\n DispatchQueue.main.async() {\n self.checkCamera() } }\n }\n }\n )\n present(alert, animated: true, completion: nil)\n }\n",
2016-12-22T08:42:01
pableiros :

For Swift 3, you can add this on your viewWillAppear method of your first view controller:\n\nFirst import the AVFoundation framework\n\nimport AVFoundation\n\n\nThen:\n\noverride func viewWillAppear(_ animated: Bool) {\n super.viewWillAppear(animated)\n\n let authorizationStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)\n\n switch authorizationStatus {\n case .notDetermined:\n AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { granted in\n if granted {\n print(\"access granted\")\n }\n else {\n print(\"access denied\")\n }\n }\n case .authorized:\n print(\"Access authorized\")\n case .denied, .restricted:\n print(\"restricted\")\n\n }\n}\n\n\nDon't forget to add Privacy - Camera Usage Description key on your Info.plist",
2016-11-14T16:35:57
Michael :

For me this work on iOS7 and iOS8:\n\n ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];\n\n switch (status) {\n case ALAuthorizationStatusAuthorized:\n break;\n\n case ALAuthorizationStatusRestricted:\n case ALAuthorizationStatusDenied:\n break;\n\n case ALAuthorizationStatusNotDetermined:\n break;\n }\n",
2015-03-02T23:12:15
Alvin George :

I make an access check on the app delegate.\n\nimport UIKit\nimport AVFoundation\nimport Photos\n\n func applicationDidBecomeActive(application: UIApplication) {\n cameraAllowsAccessToApplicationCheck()\n internetAvailabilityOnApplicationCheck()\n photoLibraryAvailabilityCheck()\n }\n\n //MARK:- CAMERA ACCESS CHECK\n func cameraAllowsAccessToApplicationCheck()\n {\n let authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)\n switch authorizationStatus {\n case .NotDetermined:\n // permission dialog not yet presented, request authorization\n AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo,\n completionHandler: { (granted:Bool) -> Void in\n if granted {\n print(\"access granted\")\n }\n else {\n print(\"access denied\")\n }\n })\n case .Authorized:\n print(\"Access authorized\")\n case .Denied, .Restricted:\n alertToEncourageCameraAccessWhenApplicationStarts()\n default:\n print(\"DO NOTHING\")\n }\n }\n //MARK:- PHOTO LIBRARY ACCESS CHECK\n func photoLibraryAvailabilityCheck()\n {\n if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.Authorized\n {\n\n }\n else\n {\n var cameraUnavailableAlertController = UIAlertController (title: \"Photo Library Unavailable\", message: \"Please check to see if device settings doesn't allow photo library access\", preferredStyle: .Alert)\n\n var settingsAction = UIAlertAction(title: \"Settings\", style: .Destructive) { (_) -> Void in\n let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)\n if let url = settingsUrl {\n UIApplication.sharedApplication().openURL(url)\n }\n }\n var cancelAction = UIAlertAction(title: \"Okay\", style: .Default, handler: nil)\n cameraUnavailableAlertController .addAction(settingsAction)\n cameraUnavailableAlertController .addAction(cancelAction)\n self.window?.rootViewController!.presentViewController(cameraUnavailableAlertController , animated: true, completion: nil)\n }\n }\n func internetAvailabilityOnApplicationCheck()\n {\n //MARK:- INTERNET AVAILABLITY\n if InternetReachability.isConnectedToNetwork() {\n\n }\n else\n {\n dispatch_async(dispatch_get_main_queue(), {\n\n //INTERNET NOT AVAILABLE ALERT\n var internetUnavailableAlertController = UIAlertController (title: \"Network Unavailable\", message: \"Please check your internet connection settings and turn on Network Connection\", preferredStyle: .Alert)\n\n var settingsAction = UIAlertAction(title: \"Settings\", style: .Destructive) { (_) -> Void in\n let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)\n if let url = settingsUrl {\n UIApplication.sharedApplication().openURL(url)\n }\n }\n var cancelAction = UIAlertAction(title: \"Okay\", style: .Default, handler: nil)\n internetUnavailableAlertController .addAction(settingsAction)\n internetUnavailableAlertController .addAction(cancelAction)\n self.window?.rootViewController!.presentViewController(internetUnavailableAlertController , animated: true, completion: nil)\n })\n }\n }\n\n\n*",
2015-10-29T09:25:17
jamix :

Here is the approach we ended up using:\n\nif ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) {\n [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {\n // Will get here on both iOS 7 & 8 even though camera permissions weren't required \n // until iOS 8. So for iOS 7 permission will always be granted.\n if (granted) {\n // Permission has been granted. Use dispatch_async for any UI updating\n // code because this block may be executed in a thread.\n dispatch_async(dispatch_get_main_queue(), ^{\n [self doStuff];\n }); \n } else {\n // Permission has been denied.\n }\n }];\n} else {\n // We are on iOS <= 6. Just do what we need to do.\n [self doStuff];\n}\n",
2014-09-27T06:45:19
SmokersCough :

I'm running into a similar issue, if the user has denied camera access when they are first prompted, pressing the button to take snapshot results in a black screen in the camera mode.\nHowever I want to detect that the user has declined access and prompt them it must be turned on but I can't find any functions to check the current user camera access, is there such a function?\nEDIT: The following check will let you know in IOS 8 about camera access:\n#import <AVFoundation/AVFoundation.h>\n\nAVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];\n \n if(status == AVAuthorizationStatusAuthorized) { // authorized\n \n }\n else if(status == AVAuthorizationStatusDenied){ // denied\n \n }\n else if(status == AVAuthorizationStatusRestricted){ // restricted\n \n \n }\n else if(status == AVAuthorizationStatusNotDetermined){ // not determined\n \n [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {\n if(granted){ // Access has been granted ..do something\n \n } else { // Access denied ..do something\n \n }\n }];\n }\n\nThis information was found on the following question (How to know that application have camera access or not programmatically in iOS8):",
2014-09-23T18:42:53
DogCoffee :

Here is my Swift Solution (iOS 8), I needed the camera for QR scanning so really had to prompt its use. \n\nThis provides\n\n\nEncourage the user to select allow if prior to the default allow camera access question\nEasy way to access settings if the user denied the first request.\n\n\nTo get it running call check camera in ViewDidAppear / or ViewDidLoad etc. I needed to use viewDidAppear so my custom camera views constraints were set up.\n\nfunc checkCamera() {\n let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)\n switch authStatus {\n case .authorized: break // Do your stuff here i.e. allowScanning()\n case .denied: alertToEncourageCameraAccessInitially()\n case .notDetermined: alertPromptToAllowCameraAccessViaSetting()\n default: alertToEncourageCameraAccessInitially()\n }\n}\n\nfunc alertToEncourageCameraAccessInitially() {\n let alert = UIAlertController(\n title: \"IMPORTANT\",\n message: \"Camera access required for QR Scanning\",\n preferredStyle: UIAlertControllerStyle.alert\n )\n alert.addAction(UIAlertAction(title: \"Cancel\", style: .default, handler: nil))\n alert.addAction(UIAlertAction(title: \"Allow Camera\", style: .cancel, handler: { (alert) -> Void in\n UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)\n }))\n present(alert, animated: true, completion: nil)\n}\n\nfunc alertPromptToAllowCameraAccessViaSetting() {\n\n let alert = UIAlertController(\n title: \"IMPORTANT\",\n message: \"Please allow camera access for QR Scanning\",\n preferredStyle: UIAlertControllerStyle.alert\n )\n alert.addAction(UIAlertAction(title: \"Dismiss\", style: .cancel) { alert in\n if AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count > 0 {\n AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { granted in\n DispatchQueue.main.async() {\n self.checkCamera() } }\n }\n }\n )\n present(alert, animated: true, completion: nil)\n}\n\n\nThanks to jamix above for the tip for using dispatch_async - makes the response to show the newly set camera function so much faster.\n\nSorry for a mix of trailing closures.. wanted to try them out.",
2015-06-27T08:39:34
yy