當今設備和 OS (操作係統) 的許多特徵可能對隱私、安全性、及性能有顯著影響,若濫用。因此,要求用戶明確同意的平颱越來越普遍,在訪問這些特徵之前。
Qt 權限 API 允許應用程序以跨平颱方式,校驗 (或請求) 這些特性的權限。
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).
注意: 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.
請求的每個權限都必須附有所謂的
用法描述
字符串在應用程序的
Info.plist
文件,描述應用程序為什麼需要訪問給定權限。例如:
<key>NSMicrophoneUsageDescription</key> <string>The microphone is used to record voice memos.</string>
每種權限類型的相關用法描述鍵,在文檔編製中有描述。
要確保相關權限後端包括在應用程序中,請
把構建係統指嚮自定義
Info.plist
.
請求的每個權限都必須附有
uses-permission
條目在應用程序的
AndroidManifest.xml
文件。例如:
<manifest ...>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
</manifest>
要確保相關權限後端包括在應用程序中,請
把構建係統指嚮自定義
AndroidManifest.xml
或使用 qt_add_android_permission()。
每種權限類型的相關權限名稱,在文檔編製中有描述。
下列權限類型可用:
| 訪問藍牙外圍設備 | |
| 訪問用戶的日曆 | |
| 訪問攝像頭為取得圖片 (或視頻) | |
| 訪問用戶的聯係人 | |
| 訪問用戶的位置 | |
| 為監視訪問麥剋風 (或錄製聲音) |
注意: The available permission types cover core functionality of Qt modules like Qt Multimedia and Qt Positioning, but do not encompass all platform-specific permissions. Custom permission types are not currently supported.
To ensure the best possible user experience for the end user we recommend adopting the following best practices for managing application permissions:
另請參閱 信息特性列錶文件 and Qt Creator:編輯清單文件 .