Many features of today's devices and operating systems can have significant privacy, security, and performance implications if misused. It's therefore increasingly common for platforms to require explicit consent from the user before accessing these features.
The Qt permission APIs allow the application to check or request permission for such features in a cross platform manner.
A feature that commonly requires user consent is access to the microphone of the device. An application for recording voice memos would perhaps look something like this initially:
void VoiceMemoWidget::onRecordingInitiated() { m_microphone->startRecording(); }
To ensure this application works well on platforms that require user consent for microphone access we would extend it like this:
void VoiceMemoWidget::onRecordingInitiated() { QMicrophonePermission microphonePermission; switch (qApp->checkPermission(microphonePermission)) { case Qt::PermissionStatus::Undetermined: qApp->requestPermission(microphonePermission, this, &VoiceMemoWidget::onRecordingInitiated); return; case Qt::PermissionStatus::Denied: m_permissionInstructionsDialog->show(); return; case Qt::PermissionStatus::Granted: m_microphone->startRecording(); } }
We first check if we already know the status of the microphone permission. If we don't we initiate a permission request to determine the current status, which will potentially ask the user for consent. We connect the result of the request to the slot we're already in, so that we get another chance at evaluating the permission status.
Once the permission status is known, either because we had been granted or denied permission at an earlier time, or after getting the result back from the request we just initiated, we redirect the user to a dialog explaining why we can not record voice memos at this time (if the permission was denied), or proceed to using the microphone (if permission was granted).
Note: On macOS and iOS permissions can currently only be requested for GUI applications.
Some platforms require that the permissions you request are declared up front at build time.
Each permission you request must be accompanied by a so called usage description string in the application's Info.plist file, describing why the application needs to access the given permission. For example:
<key>NSMicrophoneUsageDescription</key> <string>The microphone is used to record voice memos.</string>
The relevant usage description keys are described in the documentation for each permission type.
To ensure the relevant permission backend is included with your application, please point the build system to your custom Info.plist.
Each permission you request must be accompanied by a uses-permission entry in the application's AndroidManifest.xml file. For example:
<manifest ...>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
</manifest>
To ensure the relevant permission backend is included with your application, please point the build system to your custom AndroidManifest.xml.
The relevant permission names are described in the documentation for each permission type.
The following permissions types are available:
|
Access Bluetooth peripherals |
|
|
Access the user's calendar |
|
|
Access the camera for taking pictures or videos |
|
|
Access the user's contacts |
|
|
Access the user's location |
|
|
Access the microphone for monitoring or recording sound |
To ensure the best possible user experience for the end user we recommend adopting the following best practices for managing application permissions:
See also Information Property List Files and Qt Creator: Editing Manifest Files.