Requests the user’s permission, if needed, for recording a specified media type.
SDKs
- iOS 7.0+
- macOS 10.14+
- Mac Catalyst 13.0+
Framework
- AVFoundation
Declaration
+ (void)requestAccessForMediaType:(AVMedia Type)mediaType completionHandler:(void (^)(BOOL granted))handler;
Parameters
mediaType
A media type constant, either
AVMedia
orType Video AVMedia
. If no media type is supplied, anType Audio NSInvalid
will be thrown.Argument Exception 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; otherwiseNO
. The block will return immediately.
Discussion
Recording audio or video always requires explicit permission from the user. The first time you create any AVCapture
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.
Important
Your app must provide an explanation for its use of capture devices using the NSCameraUsageDescription or NSMicrophoneUsageDescription Info.plist key; iOS displays this explanation when initially asking the user for permission, and thereafter in the Settings app. Calling this method or attempting to start a capture session without a usage description raises an exception.
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 AVCapture
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 AVMedia
is equivalent to calling the AVAudio
method request
.