Skip to content
Tauri 中文网

Tauri 2.0.0-alpha.4 发布

Tauri 1.2 Launch Hero Image

2.0 的新 alpha 版本已经发布。此版本包括即将发布的 Tauri 1.3 版本的所有变更、HTTP 客户端的重要重大更改以及 Tauri 插件的原生移动功能。

🌐 A new alpha release for the 2.0 has been published. This release includes all changes from the upcoming Tauri 1.3 release, an important breaking change on the HTTP client and native mobile capabilities for Tauri plugins.

🌐 Updating dependencies

确保将 NPM 和 Cargo 的依赖更新到最新的 alpha 版本。你可以通过以下方式更新 NPM 依赖:

🌐 Make sure to update both NPM and Cargo dependencies to the latest alpha release. You can update the NPM dependencies with:

npm install @tauri-apps/cli@next @tauri-apps/api@next

重新创建移动项目以使用新功能:

🌐 Recreate the mobile projects to use the new features:

Terminal window
rm -r src-tauri/gen
tauri android init
tauri ios init

🌐 HTTP Client Breaking Change

由于 Windows 上开发服务器代理的问题,使用 attohttpc 的默认 HTTP 客户端已被移除。所有 reqwest-* 功能标志已被移除,因为 reqwest 现在是我们使用的客户端。

🌐 The default HTTP client using attohttpc has been removed due to issues with the development server proxy on Windows. All reqwest-* feature flags have been removed because reqwest is now the client we use.

🌐 Native Mobile Functionality for Tauri Plugins

Tauri 插件现在可以通过 Swift 访问 iOS,通过 Kotlin 或 Java 代码访问 Android API,从而简化对平台接口(如相机或地理位置)的使用。要在现有插件上启动 iOS 和 Android 项目,请运行 tauri plugin ios addtauri plugin android add。新插件会自动包含编写原生移动代码所需的所有配置。

🌐 A Tauri plugin now can access iOS via Swift and Android APIs via Kotlin or Java code, simplifying usage of platform interfaces such as camera or geolocation. To bootstrap the iOS and Android projects on an existing plugin, run tauri plugin ios add and tauri plugin android add. New plugins automatically include all the configuration needed to write native mobile code.

这是一个插件示例,它接收一个字符串值并解析为一个对象:

🌐 Here’s an example of a plugin that takes a string value and resolves an object:

安卓插件:

ExamplePlugin.kt
package com.plugin.example
import android.app.Activity
import app.tauri.annotation.Command
import app.tauri.annotation.TauriPlugin
import app.tauri.plugin.JSObject
import app.tauri.plugin.Plugin
import app.tauri.plugin.Invoke
@TauriPlugin
class ExamplePlugin(private val activity: Activity): Plugin(activity) {
@Command
fun ping(invoke: Invoke) {
val value = invoke.getString("value") ?: ""
val ret = JSObject()
ret.put("value", value)
invoke.resolve(ret)
}
}

iOS 插件:

ExamplePlugin.swift
import UIKit
import WebKit
import Tauri
class ExamplePlugin: Plugin {
@objc public func ping(_ invoke: Invoke) throws {
let value = invoke.getString("value")
invoke.resolve(["value": value as Any])
}
}
@_cdecl("init_plugin_example")
func initPlugin(name: SRString, webview: WKWebView?) {
Tauri.registerPlugin(webview: webview, name: name.toString(), plugin: ExamplePlugin())
}

初始化插件的 Rust 代码:

use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_example);
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("example")
.setup(|app, api| {
#[cfg(target_os = "android")]
api.register_android_plugin("com.plugin.example", "ExamplePlugin")?;
#[cfg(target_os = "ios")]
api.register_ios_plugin(init_plugin_example)?;
Ok(())
})
.build()
}

调用插件命令的前端代码:

import { invoke } from '@tauri-apps/api/tauri';
invoke('plugin:example|ping', { value: 'Tauri' }).then(({ value }) =>
console.log('Response', value)
);

查看即将推出的 相机插件路径插件

🌐 Check out the upcoming camera plugin and path plugin.


Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站