自动启动
在系统启动时自动启动应用程序。
支持的平台
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | |
ios | |
设置
请安装 autostart 插件。
使用项目的包管理器来添加依赖。
npm run tauri add autostart
yarn run tauri add autostart
pnpm tauri add autostart
bun tauri add autostart
cargo tauri add autostart
-
运行
cargo add tauri-plugin-autostart
命令,将插件添加到项目的Cargo.toml
依赖中。 -
修改
lib.rs
来初始化插件。
use tauri_plugin_autostart::MacosLauncher;
#[cfg_attr(mobile, tauri::mobile_entry_point)]fn run() { tauri::Builder::default() .plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, Some(vec!["--flag1", "--flag2"]) /* arbitrary number of args to pass to your app */)) .run(tauri::generate_context!()) .expect("error while running tauri application");}
- 你可以使用你喜欢的 JavaScript 包管理器来安装 JavaScript Guest 绑定。
npm install @tauri-apps/plugin-autostart
yarn add @tauri-apps/plugin-autostart
pnpm add @tauri-apps/plugin-autostart
bun add @tauri-apps/plugin-autostart
用法
autostart 插件有 JavaScript 和 Rust 两种版本。
import { enable, isEnabled, disable } from '@tauri-apps/plugin-autostart';
// 启用 autostartawait enable();// 检查 enable 状态console.log(`registered for autostart? ${await isEnabled()}`);// 禁用 autostartdisable();
use tauri_plugin_autostart::MacosLauncher;use tauri_plugin_autostart::ManagerExt;
#[cfg_attr(mobile, tauri::mobile_entry_point)]fn run() { tauri::Builder::default() .plugin(tauri_plugin_autostart::init( MacosLauncher::LaunchAgent, Some(vec!["--flag1", "--flag2"]), )) .setup(|app| { // Get the autostart manager let autostart_manager = app.autolaunch(); // 启用 autostart let _ = autostart_manager.enable(); // 检查 enable 状态 println!("registered for autostart? {}", autostart_manager.is_enabled().unwrap()); // 禁用 autostart let _ = autostart_manager.disable(); Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}
权限
默认情况下,所有插件命令都被阻止,无法访问。
你必须在你的 capabilities
配置中定义一个权限列表。
更多信息请参见访问控制列表。
{ "permissions": [ ..., "autostart:allow-enable", "autostart:allow-disable", "autostart:allow-is-enabled" ]}
权限 | 描述 |
---|---|
autostart:allow-disable | 启用 disable 命令,不需要预先配置作用域。 |
autostart:deny-disable | 拒绝没有预先配置作用域的 disable 命令。 |
autostart:allow-enable | 启用 enable 命令,不需要预先配置作用域。 |
autostart:deny-enable | 拒绝使用没有预先配置作用域的 enable 命令。 |
autostart:allow-is-enabled | 启用没有预先配置作用域的 is_enabled 命令。 |
autostart:deny-is-enabled | 拒绝没有预先配置作用域的 is_enabled 命令。 |
© 2025 Tauri Contributors. CC-BY / MIT