Presenting camera permission dialog in iOS 8

后端 未结 9 1458
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 17:45

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

相关标签:
9条回答
  • 2020-11-29 18:38

    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];
    }
    
    0 讨论(0)
  • 2020-11-29 18:39

    I'm running into a similar issue, if the user has denied camera access when they are first prompted, pressing the button to take a snap shot results in a black screen in the camera mode.

    However 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?

    EDIT: The following check will let you know in IOS 8 about camera access:

    #import <AVFoundation/AVFoundation.h>
    
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
        if(status == AVAuthorizationStatusAuthorized) { // authorized
    
        }
        else if(status == AVAuthorizationStatusDenied){ // denied
    
        }
        else if(status == AVAuthorizationStatusRestricted){ // restricted
    
    
        }
        else if(status == AVAuthorizationStatusNotDetermined){ // not determined
    
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                if(granted){ // Access has been granted ..do something
    
                } else { // Access denied ..do something
    
                }
            }];
        }
    

    This information was found on the following question (How to know that application have camera access or not programmatically in iOS8):

    0 讨论(0)
  • 2020-11-29 18:43

    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.

    Since 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.

        AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in
            if (videoGranted) {
                AVCaptureDevice.requestAccessForMediaType(AVMediaTypeAudio, completionHandler: { (audioGranted: Bool) -> Void in
                    if (audioGranted) {
                        dispatch_async(dispatch_get_main_queue()) {
                            // Both video & audio granted
                        }
                    } else {
                        // Rejected audio
                    }
                })
            } else {
                // Rejected video
            }
        })
    
    0 讨论(0)
提交回复
热议问题