深度链接(Deep Linking)
将你的 Tauri 应用程序设置为 URL 的默认处理程序。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | Deep links must be registered in config. Dynamic registration at runtime is not supported. | |
| android | Deep links must be registered in config. Dynamic registration at runtime is not supported. | |
| ios | Deep links must be registered in config. Dynamic registration at runtime is not supported. |
请安装 deep-link 插件来开始使用。
使用项目的包管理器来添加依赖:
npm run tauri add deep-linkyarn run tauri add deep-linkpnpm tauri add deep-linkdeno task tauri add deep-linkbun tauri add deep-linkcargo tauri add deep-link-
在
src-tauri文件夹中运行以下命令,将插件添加到Cargo.toml中的项目依赖项中:cargo add tauri-plugin-deep-link@2.0.0 -
修改
lib.rs来初始化插件:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_deep_link::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用你偏好的 JavaScript 包管理器安装 JavaScript Guest 绑定:
npm install @tauri-apps/plugin-deep-linkyarn add @tauri-apps/plugin-deep-linkpnpm add @tauri-apps/plugin-deep-linkdeno add npm:@tauri-apps/plugin-deep-linkbun add @tauri-apps/plugin-deep-link
Android
Section titled “Android”在 Android 上有两种方式可以从链接打开你的应用:
- App Links (http/https + host, 已验证)
对于 app links,你需要一个带有
.well-known/assetlinks.json端点的服务器,该端点必须以给定格式返回文本响应:
[ { "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "$APP_BUNDLE_ID", "sha256_cert_fingerprints": [ $CERT_FINGERPRINT ] } }]其中 $APP_BUNDLE_ID 是 tauri.conf.json > identifier 上定义的值,其中 - 替换为 _,$CERT_FINGERPRINT 是你的应用签名证书的 SHA256 指纹列表,请参阅 verify Android applinks 以获取更多信息。
- 自定义 URI schemes(无需 host,无需验证)
对于像
myapp://...这样的 URI,你可以在不托管任何文件的情况下声明自定义 scheme。在移动端配置中使用scheme字段并省略host。
在 iOS 上有两种方式可以从链接打开你的应用:
- Universal Links (https + host, 已验证)
对于 universal links,你需要一个带有
.well-known/apple-app-site-association端点的服务器,该端点必须以给定格式返回 JSON 响应:
{ "applinks": { "details": [ { "appIDs": ["$DEVELOPMENT_TEAM_ID.$APP_BUNDLE_ID"], "components": [ { "/": "/open/*", "comment": "Matches any URL whose path starts with /open/" } ] } ] }}其中 $DEVELOPMENT_TEAM_ID 是 tauri.conf.json > bundle > iOS > developmentTeam 或 TAURI_APPLE_DEVELOPMENT_TEAM 环境变量上定义的值,$APP_BUNDLE_ID 是 tauri.conf.json > identifier 上定义的值。
要验证你的域名是否正确配置以暴露应用关联,可以运行以下命令,将 <host> 替换为你的实际主机:
curl -v https://app-site-association.cdn-apple.com/a/v1/<host>请参阅 applinks.details 以获取更多信息。
- 自定义 URI schemes(无需 host,无需验证)
对于像
myapp://...这样的 URI,你可以在移动端配置下声明自定义 scheme,设置"appLink": false(或省略该字段)。插件会在你应用的 Info.plist 中生成相应的CFBundleURLTypes条目。无需.well-known文件或 HTTPS 主机。
桌面端(Desktop)
Section titled “桌面端(Desktop)”在 Linux 和 Windows 上,深度链接以命令行参数的形式传递给新的应用进程。 如果你希望使用唯一的应用实例来接收事件,深度链接插件可以与 single instance 插件集成。
- 首先,你必须为单实例插件添加
deep-link特性:
[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\"))".dependencies]tauri-plugin-single-instance = { version = "2.0.0", features = ["deep-link"] }- 然后配置单实例插件,该插件应始终是你注册的第一个插件:
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { let mut builder = tauri::Builder::default();
#[cfg(desktop)] { builder = builder.plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| { println!("a new app instance was opened with {argv:?} and the deep link event was already triggered"); // 在运行时定义深度链接 schemes 时,你还必须在此处检查 `argv` })); }
builder = builder.plugin(tauri_plugin_deep_link::init());}在 tauri.conf.json > plugins > deep-link 下,配置你想要与应用程序关联的移动端域名/scheme 和桌面端 scheme。
移动端自定义 scheme(无需服务器):
{ "plugins": { "deep-link": { "mobile": [ { "scheme": ["ovi"], "appLink": false } ] } }}这将在 Android 和 iOS 上注册 ovi://* scheme。
App Link / Universal Link(已验证的 https + host):
{ "plugins": { "deep-link": { "mobile": [ { "scheme": ["https"], "host": "your.website.com", "pathPrefix": ["/open"], "appLink": true } ] } }}这将注册 https://your.website.com/open/* 为 app/universal link。
桌面端自定义 scheme:
{ "plugins": { "deep-link": { "desktop": { "schemes": ["something", "my-tauri-app"] } } }}deep-link 插件有 JavaScript 和 Rust 两种版本。
监听深度链接
Section titled “监听深度链接”当深度链接在应用运行时触发时,会调用 onOpenUrl 回调。要检测你的应用是否是通过深度链接打开的,请在应用启动时使用 getCurrent。
import { getCurrent, onOpenUrl } from '@tauri-apps/plugin-deep-link';// 当使用 `"withGlobalTauri": true` 时,你可以使用// const { getCurrent, onOpenUrl } = window.__TAURI__.deepLink;
const startUrls = await getCurrent();if (startUrls) { // 应用很可能是通过深度链接启动的 // 注意:getCurrent 的返回值在每次 onOpenUrl 被触发时也会更新}
await onOpenUrl((urls) => { console.log('deep link:', urls);});当深度链接在应用运行时触发时,会调用插件的 on_open_url 闭包。要检测你的应用是否是通过深度链接打开的,请在应用启动时使用 get_current。
use tauri_plugin_deep_link::DeepLinkExt;
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_deep_link::init()) .setup(|app| { // 注意:get_current 的返回值在每次 on_open_url 被触发时也会更新 let start_urls = app.deep_link().get_current()?; if let Some(urls) = start_urls { // 应用很可能是通过深度链接启动的 println!("deep link URLs: {:?}", urls); }
app.deep_link().on_open_url(|event| { println!("deep link URLs: {:?}", event.urls()); }); Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}在运行时注册桌面端深度链接
Section titled “在运行时注册桌面端深度链接”配置 部分描述了如何为你的应用定义静态深度链接 schemes。
在 Linux 和 Windows 上,还可以通过 register Rust 函数在运行时将 schemes 与你的应用关联。
在以下代码段中,我们将在运行时注册 my-app scheme。首次执行应用后,操作系统会将 my-app://* URL 打开到我们的应用中:
use tauri_plugin_deep_link::DeepLinkExt;
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_deep_link::init()) .setup(|app| { #[cfg(desktop)] app.deep_link().register("my-app")?; Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}测试应用的深度链接有一些注意事项。
深度链接仅对已安装的桌面应用触发。
在 Linux 和 Windows 上,你可以使用 register_all Rust 函数来绕过此限制,该函数会注册所有已配置的 schemes 以触发当前可执行文件:
use tauri_plugin_deep_link::DeepLinkExt;
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_deep_link::init()) .setup(|app| { #[cfg(any(windows, target_os = "linux"))] { use tauri_plugin_deep_link::DeepLinkExt; app.deep_link().register_all()?; } Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}Windows
Section titled “Windows”要在 Windows 上触发深度链接,你可以在浏览器中打开 <scheme>://url,或在终端中运行以下命令:
start <scheme>://url要在 Linux 上触发深度链接,你可以在浏览器中打开 <scheme>://url,或在终端中运行 xdg-open:
xdg-open <scheme>://url要在 iOS 上触发应用链接,你可以在浏览器中打开 https://<host>/path URL。对于模拟器,你可以使用 simctl CLI 直接从终端打开链接:
xcrun simctl openurl booted https://<host>/pathAndroid
Section titled “Android”要在 Android 上触发应用链接,你可以在浏览器中打开 https://<host>/path URL。对于模拟器,你可以使用 adb CLI 直接从终端打开链接:
adb shell am start -a android.intent.action.VIEW -d https://<host>/path <bundle-identifier>默认情况下,所有潜在危险的插件命令和作用域都被阻止,无法访问。你必须在你的 capabilities 配置中修改权限以启用它们。
更多信息请参见能力概述和使用插件权限的逐步指南。
{ "$schema": "../gen/schemas/mobile-schema.json", "identifier": "mobile-capability", "windows": ["main"], "platforms": ["iOS", "android"], "permissions": [ // 通常你需要 core:event:default 来监听深度链接事件 "core:event:default", "deep-link:default" ]}Default Permission
Allows reading the opened deep link via the get_current command
This default permission set includes the following:
allow-get-current
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the get_current command without any pre-configured scope. |
|
|
Denies the get_current command without any pre-configured scope. |
|
|
Enables the is_registered command without any pre-configured scope. |
|
|
Denies the is_registered command without any pre-configured scope. |
|
|
Enables the register command without any pre-configured scope. |
|
|
Denies the register command without any pre-configured scope. |
|
|
Enables the unregister command without any pre-configured scope. |
|
|
Denies the unregister command without any pre-configured scope. |
© 2026 Tauri Contributors. CC-BY / MIT