Requestaccessformediatype Doesn't Ask for Permission

requestAccessForMediaType doesn't ask for permission

If it's taking a long time to appear, then it sounds like you're not running this on the main thread, which you should be. Also, if it has prompted once before, it won't prompt again – the user has to go into Settings and enable camera access.

AVCaptureDevice.requestAccessForMediaType never pops up window and always return false

Problem solved. It seems that when we upgrade from iOS 8 to iOS 9, it has a new property in info.plist which is Bundle Display Name. Set the name to your product name if it is null, or the system will never know what app is asking for a permission

Why my iOS app does not ask user permission to access camera?

The problem is solved now. I need to trace back step by step and took me long time to solve the problem.
Some people suggested to include
NSCamerausageDescription Key. Actually it is necessary for devices with iOS 7 and above. I included the Key but still user permission is not asked yet.
Finally I found the problem at Info.plist.
There are some extra lines and I deleted three lines. Not sure how they are related to my problem.
Those I deleted are

(1)Get info string

(2)Bundle display name

(3)Application Category

After deleting these three lines in the info.plist. Then the app asks the user permission. The point is that you may need to check your Info.plist for such problem.

Is there a way to ask user for Camera access after they have already denied it on iOS?

After some research it looks like you can't do what I'd like. Here is the alternative that I coded to pop a dialog and open the Settings app automatically if on iOS 8+.

Some notes:

  • Since iOS 10 you need to specify NSCameraUsageDescription key in your Info.plist to be able ask for camera access, otherwise your app will crash at runtime.
  • Once the user changes any permissions for your app, it will kill your app. Handle accordingly and save any needed data before the user hits that "Go" button.
  • At some point between iOS 8 and 11, Apple no longer required the user to touch the Privacy cell in the Setting apps in order to get to and change the Camera settings. You may want to change your instructions on what the user is supposed to do in the Settings app based on what iOS version they are using. If someone wants to leave a comment below telling us all what exact iOS version that changed in, that would be awesome.
  • As of the last edit of this answer, the code below is working on iOS 14.2.

Swift 5.2:

At the top of your view controller:

import AVFoundation

Before opening the camera view:

@IBAction func goToCamera()
{
let status = AVCaptureDevice.authorizationStatus(for: .video)
switch (status)
{
case .authorized:
self.popCamera()

case .notDetermined:
AVCaptureDevice.requestAccess(for: .video) { (granted) in
if (granted)
{
self.popCamera()
}
else
{
self.camDenied()
}
}

case .denied:
self.camDenied()

case .restricted:
let alert = UIAlertController(title: "Restricted",
message: "You've been restricted from using the camera on this device. Without camera access this feature won't work. Please contact the device owner so they can give you access.",
preferredStyle: .alert)

let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
@unknown default:
fatalError()
}
}

Denial alert with completion block:

func camDenied()
{
DispatchQueue.main.async
{
var alertText = "It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Close this app.\n\n2. Open the Settings app.\n\n3. Scroll to the bottom and select this app in the list.\n\n4. Turn the Camera on.\n\n5. Open this app and try again."

var alertButton = "OK"
var goAction = UIAlertAction(title: alertButton, style: .default, handler: nil)

if UIApplication.shared.canOpenURL(URL(string: UIApplication.openSettingsURLString)!)
{
alertText = "It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Touch the Go button below to open the Settings app.\n\n2. Turn the Camera on.\n\n3. Open this app and try again."

alertButton = "Go"

goAction = UIAlertAction(title: alertButton, style: .default, handler: {(alert: UIAlertAction!) -> Void in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
})
}

let alert = UIAlertController(title: "Error", message: alertText, preferredStyle: .alert)
alert.addAction(goAction)
self.present(alert, animated: true, completion: nil)
}
}

Objective-C:

At the top of your view controller:

#import <AVFoundation/AVFoundation.h>

Before opening the camera view:

- (IBAction)goToCamera
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusAuthorized)
{
[self popCamera];
}
else if(authStatus == AVAuthorizationStatusNotDetermined)
{
NSLog(@"%@", @"Camera access not determined. Ask for permission.");

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
{
if(granted)
{
NSLog(@"Granted access to %@", AVMediaTypeVideo);
[self popCamera];
}
else
{
NSLog(@"Not granted access to %@", AVMediaTypeVideo);
[self camDenied];
}
}];
}
else if (authStatus == AVAuthorizationStatusRestricted)
{
// My own Helper class is used here to pop a dialog in one simple line.
[Helper popAlertMessageWithTitle:@"Error" alertText:@"You've been restricted from using the camera on this device. Without camera access this feature won't work. Please contact the device owner so they can give you access."];
}
else
{
[self camDenied];
}
}

Denial alert:

- (void)camDenied
{
NSLog(@"%@", @"Denied camera access");

NSString *alertText;
NSString *alertButton;

BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
if (canOpenSettings)
{
alertText = @"It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Touch the Go button below to open the Settings app.\n\n2. Turn the Camera on.\n\n3. Open this app and try again.";

alertButton = @"Go";
}
else
{
alertText = @"It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Close this app.\n\n2. Open the Settings app.\n\n3. Scroll to the bottom and select this app in the list.\n\n4. Turn the Camera on.\n\n5. Open this app and try again.";

alertButton = @"OK";
}

UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:alertText
delegate:self
cancelButtonTitle:alertButton
otherButtonTitles:nil];
alert.tag = 3491832;
[alert show];
}

Delegate call for the UIAlertView:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 3491832)
{
BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
if (canOpenSettings)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}

MacOS - requesting audio/camera permission - wrong app title shown?

Unfortunately, this is actually a vulnerability that Apple has never fixed, which could be exploited by malware. This is discussed by Patrick Wardle at his site:

https://objective-see.com/blog/blog_0x2F.html

Therefore, I highly encourage you to file a bug, so that this can be fixed.

Request camera permissions in macOS Mojave

Found that the solution is actually analog to iOS by checking authorizationStatus(for:) on AVCaptureDevice before initializing an AVCaptureDeviceInput from it.

And using requestAccess(for:completionHandler:) to request the permission if needed.

An example for getting camera access:

let status = AVCaptureDevice.authorizationStatus(for: .video)

if status == .authorized {
// connect to video device
let devices = AVCaptureDevice.devices(for: .video)
...
return
}

if status == .denied {
// show error
return
}

AVCaptureDevice.requestAccess(for: .video) { (accessGranted) in
// handle result
}

Asking camera permission on iPad

You need to request for permission using the following code:

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusAuthorized)
{
// Take picture
}
else
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
{
if (granted)
{
NSLog(@"User Granted");
}
else
{
NSLog(@"User Denied");
}
}];
}

Presenting camera permission dialog in iOS 8

Here is the approach we ended up using:

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


Related Topics



Leave a reply



Submit