Type Method

requestAccessForMediaType:completionHandler:

Requests the user’s permission, if needed, for recording a specified media type.

Declaration

+ (void)requestAccessForMediaType:(AVMediaType)mediaType completionHandler:(void (^)(BOOL granted))handler;

Parameters

mediaType

A media type constant, either AVMediaTypeVideo or AVMediaTypeAudio. If no media type is supplied, an NSInvalidArgumentException will be thrown.

handler

A block to be called once permission is granted or denied.

The completion handler is called on an arbitrary dispatch queue. It is the client's responsibility to ensure that any UIKit-related updates are called on the main queue or main thread as a result.

The block receives the following parameter:

granted

If the user grants permission to use the hardware YES is returned; otherwise NO. The block will return immediately.

Discussion

Recording audio or video always requires explicit permission from the user. The first time you create any AVCaptureDeviceInput objects for a media type that requires permission, the system automatically displays an alert to request recording permission. Alternatively, you can can call this method to prompt the user at a time of your choosing.

This call will not block while the user is being asked for access, allowing the client to continue running. Until access has been granted, any AVCaptureDevices for the media type will vend silent audio samples or black video frames. The user is only asked for permission the first time the client requests access. Later calls use the permission granted by the user.

After the user grants recording permission, the system remembers the choice for future use in the same app, but the user can change this choice at any time using the Settings app. If the user has denied your app recoding permission or has not yet responded to the permission prompt, any audio recordings will contain only silence and any video recordings will contain only black frames.

The response parameter is a block whose sole parameter indicates whether the user granted or denied permission to record. This method always returns immediately. If the user has previously granted or denied recording permission, it executes the block when called; otherwise, it displays an alert and executes the block only after the user has responded to the alert.

Invoking this method with AVMediaTypeAudio is equivalent to calling the AVAudioSession method requestRecordPermission:.

See Also

Verifying Authorization

authorizationStatusForMediaType:

Returns a constant indicating whether the app has permission for recording a specified media type

AVAuthorizationStatus

Constants that provide information regarding permission to use media capture devices.