Jan 6, 2023
Comments
Many features of today's devices and operating systems can have significant privacy, security, and performance implications, if misused. As a result, it's increasingly common for platforms to require explicit consent from the user before accessing these features.
With Qt 6.5, we've added cross platform permission APIs, that allow the application to check or request permission for such features, with backend implementations for macOS, iOS, Android, and WebAssembly.
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()
{
#if QT_CONFIG(permissions)
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:
break; // Proceed
}
#endif
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).
The use of the QT_CONFIG(permissions) macro ensures that the code will work as before on platforms where permissions are not available.
Some platforms require that the permissions you request are declared up front at build time.
On Apple platforms each permission you request must be accompanied by a so called usage description string in the application's Info.plist file. For example:
<key>NSMicrophoneUsageDescription</key>
<string>The microphone is used to record voice memos.</string>
While on Android the 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>
The relevant permission requirements are described in the documentation for each permission type.
The following permissions types are available in Qt 6.5:
To ensure the best possible user experience for the end user we recommend adopting the following best practices for managing application permissions:
For now this API are C++ only, but we're investigating QML APIs that provide the same functionality. We are also looking into adding more permission types down the line.
Please try these APIs out in the Qt 6.5 betas, and let us know of any issues you might encounter!
Download the latest release here: www.qt.io/download
Qt 6.10 is now available, with new features and improvements for application developers and device creators.
Check out all our open positions here and follow us on Instagram to see what it's like to be #QtPeople.
Qt World Summit 2026 is moving to a virtual format. After careful..
Read Article
Some of you are following our works to improve connectivity of Qt-based..
Read Article
To make it easier to access the latest AI capabilities, we have updated..
Read Article