tauri-plugin-macos-permissions

repository·master·Indexed 41 days ago

https://github.com/ayangweb/tauri-plugin-macos-permissions

A Tauri v2 plugin providing a bridge to macOS system permission APIs. It allows developers to check and request permissions for accessibility, full disk access, screen recording, microphone, camera, and input monitoring using Rust and JavaScript APIs. Version 2.3.0.

Tokens
10.2K
Snippets
34
Records
36
Agent score
48%

What's inside tauri-plugin-macos-permissions

  1. Install tauri-plugin-macos-permissions

    master

    To use this plugin, you must install both the Rust backend dependency and the JavaScript guest bindings.

    1. Add the Rust plugin to your Tauri project:

      cargo add tauri-plugin-macos-permissions
    2. Install the JavaScript API package using your preferred package manager:

      pnpm add tauri-plugin-macos-permissions-api

    Note: This plugin is designed specifically for Tauri v2.

    # Install Rust dependency
    cargo add tauri-plugin-macos-permissions
    
    # Install JS API
    pnpm add tauri-plugin-macos-permissions-api
  2. Configure Info.plist for Microphone and Camera access

    master

    If your application requires access to the microphone or camera, you must provide usage descriptions in your src-tauri/Info.plist file. Failure to do so will result in the permission requests failing or the app being rejected by macOS.

    Add the following keys to your Info.plist:

    • NSMicrophoneUsageDescription: A string explaining why your app needs microphone access.
    • NSCameraUsageDescription: A string explaining why your app needs camera access.
    <key>NSMicrophoneUsageDescription</key>
    <string>Describe why your app needs to use microphone permission</string>
    <key>NSCameraUsageDescription</key>
    <string>Describe why your app needs to use camera permissions</string>
  3. Configure tauri-plugin-macos-permissions in Tauri v2

    master

    After installing the dependencies, you must register the plugin in your Rust entry point and grant permissions in your capabilities configuration.

    1. Register the plugin in Rust

    In your src-tauri/src/lib.rs, initialize the plugin within the tauri::Builder:

    pub fn run() {
        tauri::Builder::default()
            .plugin(tauri_plugin_macos_permissions::init())
            .run(tauri::generate_context!())
            .expect("error while running tauri application");
    }

    2. Grant permissions in Capabilities

    Update your src-tauri/capabilities/default.json to include the macos-permissions:default permission:

    {
        "permissions": [
            "macos-permissions:default"
        ]
    }
    // src-tauri/src/lib.rs
    .plugin(tauri_plugin_macos_permissions::init())
  4. Initialize the macOS Permissions plugin in Tauri

    master

    To use the macOS permissions functionality in your Tauri application, you must register the plugin in your main.rs or lib.rs using the init() function. This registers the available permission commands (Accessibility, Full Disk Access, Screen Recording, Microphone, Camera, and Input Monitoring) with the Tauri runtime.

    use tauri_plugin_macos_permissions::init;
    
    fn main() {
        tauri::Builder::default()
            .plugin(init())
            .run(tauri::generate_context!())
            .expect("error while running tauri application");
    }
  5. Run the plugin example project

    master

    To explore the plugin in a working environment, you can clone the repository and run the provided example application:

    1. Clone the repo: git clone https://github.com/ayangweb/tauri-plugin-macos-permissions.git
    2. Install dependencies and build the plugin: pnpm install && pnpm build
    3. Navigate to the example app and run it: cd examples/tauri-app && pnpm install && pnpm tauri dev
    git clone https://github.com/ayangweb/tauri-plugin-macos-permissions.git
    pnpm install
    pnpm build
    cd examples/tauri-app
    pnpm install
    pnpm tauri dev
  6. Use macOS permission APIs in JavaScript

    master

    Once configured, you can import the permission methods from tauri-plugin-macos-permissions-api to check or request system permissions.

    Available Methods

    MethodDescription
    checkAccessibilityPermissionCheck accessibility permission
    requestAccessibilityPermissionRequest accessibility permission
    checkFullDiskAccessPermissionCheck full disk access permission
    requestFullDiskAccessPermissionRequest full disk access permission
    checkScreenRecordingPermissionCheck screen recording permission
    requestScreenRecordingPermissionRequest screen recording permission
    checkMicrophonePermissionCheck microphone permission
    requestMicrophonePermissionRequest microphone permission
    checkCameraPermissionCheck camera permission
    requestCameraPermissionRequest camera permission
    checkInputMonitoringPermissionCheck input monitoring permission
    requestInputMonitoringPermissionRequest input monitoring permission

    Example Usage

    import { checkAccessibilityPermission } from "tauri-plugin-macos-permissions-api";
    
    const authorized = await checkAccessibilityPermission();
    console.log(authorized); // true or false
    import { checkAccessibilityPermission } from "tauri-plugin-macos-permissions-api";
    
    const authorized = await checkAccessibilityPermission();
    console.log(authorized);
  7. Reference the macOS permission identifiers

    master

    When configuring permissions in your Tauri application, use the following identifiers. Each identifier follows the pattern macos-permissions:allow-[command] or macos-permissions:deny-[command] to enable or disable specific capabilities without a pre-configured scope.

    | Identifier | Description |
    | --- | --- |
    | `macos-permissions:allow-check-accessibility-permission` | Enables the check_accessibility_permission command without any pre-configured scope. |
    | `macos-permissions:deny-check-accessibility-permission` | Denies the check_accessibility_permission command without any pre-configured scope. |
    | `macos-permissions:allow-check-camera-permission` | Enables the check_camera_permission command without any pre-configured scope. |
    | `macos-permissions:deny-check-camera-permission` | Denies the check_camera_permission command without any pre-configured scope. |
    | `macos-permissions:allow-check-full-disk-access-permission` | Enables the check_full_disk_access_permission command without any pre-configured scope. |
    | `macos-permissions:deny-check-full-disk-access-permission` | Denies the check_full_disk_access_permission command without any pre-configured scope. |
    | `macos-permissions:allow-check-input-monitoring-permission` | Enables the check_input_monitoring_permission command without any pre-configured scope. |
    | `macos-permissions:deny-check-input-monitoring-permission` | Denies the check_input_monitoring_permission command without any pre-configured scope. |
    | `macos-permissions:allow-check-microphone-permission` | Enables the check_microphone_permission command without any pre-configured scope. |
    | `macos-permissions:deny-check-microphone-permission` | Denies the check_microphone_permission command without any pre-configured scope. |
    | `macos-permissions:allow-check-screen-recording-permission` | Enables the check_screen_recording_permission command without any pre-configured scope. |
    | `macos-permissions:deny-check-screen-recording-permission` | Denies the check_screen_recording_permission command without any pre-configured scope. |
    | `macos-permissions:allow-request-accessibility-permission` | Enables the request_accessibility_permission command without any pre-configured scope. |
    | `macos-permissions:deny-request-accessibility-permission` | Denies the request_accessibility_permission command without any pre-configured scope. |
    | `macos-permissions:allow-request-camera-permission` | Enables the request_camera_permission command without any pre-configured scope. |
    | `macos-permissions:deny-request-camera-permission` | Denies the request_camera_permission command without any pre-configured scope. |
    | `macos-permissions:allow-request-full-disk-access-permission` | Enables the request_full_disk_access_permission command without any pre-configured scope. |
    | `macos-permissions:deny-request-full-disk-access-permission` | Denies the request_full_disk_access_permission command without any pre-configured scope. |
    | `macos-permissions:allow-request-input-monitoring-permission` | Enables the request_input_monitoring_permission command without any pre-configured scope. |
    | `macos-permissions:deny-request-input-monitoring-permission` | Denies the request_input_monitoring_permission command without any pre-configured scope. |
    | `macos-permissions:allow-request-microphone-permission` | Enables the request_microphone_permission command without any pre-configured scope. |
    | `macos-permissions:deny-request-microphone-permission` | Denies the request_microphone_permission command without any pre-configured scope. |
    | `macos-permissions:allow-request-screen-recording-permission` | Enables the request_screen_recording_permission command without any pre-configured scope. |
    | `macos-permissions:deny-request-screen-recording-permission` | Denies the request_screen_recording-permission command without any pre-configured scope. |
  8. Default permissions for tauri-plugin-macos-permissions

    master

    By default, the plugin includes a set of permissions that allow checking and requesting various macOS system permissions. If you are using the default configuration, the following permission types are available for use in your Tauri application:

    - `allow-check-accessibility-permission`
    - `allow-request-accessibility-permission`
    - `allow-check-full-disk-access-permission`
    - `allow-request-full-disk-access-permission`
    - `allow-check-screen-recording-permission`
    - `allow-request-screen-recording-permission`
    - `allow-check-microphone-permission`
    - `allow-request-microphone-permission`
    - `allow-check-camera-permission`
    - `allow-request-camera-permission`
    - `allow-check-input-monitoring-permission`
    - `allow-request-input-monitoring-permission`
  9. Check screen recording permission

    master

    Use check_screen_recording_permission to determine if the application currently has permission to record the screen.

    On macOS, this checks the system's screen capture access status. On non-macOS platforms, it defaults to returning true.

    ```rust
    use tauri_plugin_macos_permissions::check_screen_recording_permission;
    
    let authorized = check_screen_recording_permission().await;
    println!(
  10. Check camera permission with `check_camera_permission`

    master

    Use check_camera_permission to determine if the application currently has authorization to access the camera.

    On macOS, it checks the AVCaptureDevice authorization status for the vide media type. On other platforms, it defaults to returning true.

    use tauri_plugin_macos_permissions::check_camera_permission;
    
    let authorized = check_camera_permission().await;
    println!("Authorized: {}", authorized); // true or false
  11. Request microphone permission with `request_microphone_permission`

    master

    Use request_microphone_permission to trigger the system permission prompt for microphone access.

    On macOS, this invokes the requestAccessForMediaType method on AVCaptureDevice. This is an asynchronous operation that requests the user's consent via the standard macOS system dialog.

    Returns Ok(()) if the request was initiated, or an Err(String) if an error occurred.

    use tauri_plugin_macos_permissions::request_microphone_permission;
    
    request_microphone_permission().await.expect("Failed to request permission");