NFC
在 Android 和 iOS 设备上读取和写入 NFC 标签。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | | |
| linux | | |
| macos | | |
| android | ||
| ios |
安装 nfc 插件开始使用。
使用你项目的包管理器添加依赖:
npm run tauri add nfcyarn run tauri add nfcpnpm tauri add nfcbun tauri add nfccargo tauri add nfc-
在
src-tauri文件夹中运行以下命令,将插件添加到Cargo.toml项目的依赖项中:cargo add tauri-plugin-nfc --target 'cfg(any(target_os = "android", target_os = "ios"))' -
修改
lib.rs以初始化插件:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(mobile)]app.handle().plugin(tauri_plugin_nfc::init());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用你喜欢的 JavaScript 包管理器安装 JavaScript Guest 绑定:
npm install @tauri-apps/plugin-nfcyarn add @tauri-apps/plugin-nfcpnpm add @tauri-apps/plugin-nfcdeno add npm:@tauri-apps/plugin-nfcbun add @tauri-apps/plugin-nfc
NFC 插件需要 iOS 的原生配置。
要在 iOS 上访问 NFC API,你必须调整目标 iOS 版本,在 Info.plist 文件中配置使用描述,并将 NFC 功能添加到你的应用程序中。
NFC 插件需要 iOS 14 及以上版本。这是使用 Tauri CLI v2.8 及以上版本创建的 Tauri 应用程序的默认设置,但你可以编辑你的 Xcode 项目来配置它。
在 src-tauri/gen/apple/<project-name>.xcodeproj/project.pbxproj 文件中,将所有IPHONEOS_DEPLOYMENT_TARGET 属性设置为 14.0:
/* Begin XCBuildConfiguration section */ <random-id> /* release */ = { isa = XCBuildConfiguration; buildSettings = { ... IPHONEOS_DEPLOYMENT_TARGET = 14.0; }; name = release; }; <random-id> /* debug */ = { isa = XCBuildConfiguration; buildSettings = { ... IPHONEOS_DEPLOYMENT_TARGET = 14.0; }; name = debug; };或者您可以在 Xcode 的 General > Minimum Deployments > iOS 配置中设置部署目标。
在 iOS 上,NFC 插件需要 NFCReaderUsageDescription 信息属性列表值,该值应描述您的应用为何需要扫描或写入 NFC 标签。
在 src-tauri/Info.ios.plist 文件中,添加以下代码片段:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"> <dict> <key>NFCReaderUsageDescription</key> <string>Read and write various NFC tags</string> </dict></plist>此外,iOS 需要将 NFC 功能与你的应用程序关联。
该功能可以通过在 Xcode 的项目配置的 “Signing & Capabilities” 选项卡中点击“+ Capability”按钮并选择“Near Field Communication Tag Reading”来添加(更多信息请参阅为目标添加功能),或者通过向 gen/apple/<app-name>_iOS/<app-name>_iOS.entitlements 文件添加以下配置来添加:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict> <key>com.apple.developer.nfc.readersession.formats</key> <array> <string>TAG</string> </array></dict></plist>NFC 插件可在 JavaScript 和 Rust 中使用,允许你扫描和写入 NFC 标签。
并非每部移动设备都具备扫描 NFC 标签的功能,因此在使用扫描和写入 API 之前,你应该检查其可用性。
import { isAvailable } from '@tauri-apps/plugin-nfc';
const canScanNfc = await isAvailable();tauri::Builder::default() .setup(|app| { #[cfg(mobile)] { use tauri_plugin_nfc::NfcExt;
app.handle().plugin(tauri_plugin_nfc::init());
let can_scan_nfc = app.nfc().is_available()?; } Ok(()) })该插件可以扫描通用 NFC 标签或带有 NDEF(NFC 数据交换格式)消息的 NFC 标签,NDEF 是一种在 NFC 标签中封装类型数据的标准格式。
import { scan } from '@tauri-apps/plugin-nfc';
const scanType = { type: 'ndef', // or 'tag',};
const options = { keepSessionAlive: false, // 配置 iOS 上 "扫描 NFC" 对话框中显示的信息 message: 'Scan a NFC tag', successMessage: 'NFC tag successfully scanned',};
const tag = await scan(scanType, options);tauri::Builder::default() .setup(|app| { #[cfg(mobile)] { use tauri_plugin_nfc::NfcExt;
app.handle().plugin(tauri_plugin_nfc::init());
let tag = app .nfc() .scan(tauri_plugin_nfc::ScanRequest { kind: tauri_plugin_nfc::ScanKind::Ndef { mime_type: None, uri: None, tech_list: None, }, keep_session_alive: false, })? .tag; } Ok(()) })NFC 扫描器还可以根据特定的 URI 格式、MIME 类型或 NFC 标签技术来过滤标签。在这种情况下,扫描将仅检测符合所提供过滤器的标签。
import { scan, TechKind } from '@tauri-apps/plugin-nfc';
const techLists = [ // 使用 NfcF 捕获任意内容 [TechKind.NfcF], // 捕获所有带有 NDEF 负载的 MIFARE Classic 卡片 [TechKind.NfcA, TechKind.MifareClassic, TechKind.Ndef],];
const tag = await scan({ type: 'ndef', // or 'tag' mimeType: 'text/plain', uri: { scheme: 'https', host: 'my.domain.com', pathPrefix: '/app', }, techLists,});tauri::Builder::default() .setup(|app| { #[cfg(mobile)] { use tauri_plugin_nfc::NfcExt;
app.handle().plugin(tauri_plugin_nfc::init());
let tag = app .nfc() .scan(tauri_plugin_nfc::ScanRequest { kind: tauri_plugin_nfc::ScanKind::Ndef { mime_type: Some("text/plain".to_string()), uri: Some(tauri_plugin_nfc::UriFilter { scheme: Some("https".to_string()), host: Some("my.domain.com".to_string()), path_prefix: Some("/app".to_string()), }), tech_list: Some(vec![ vec![tauri_plugin_nfc::TechKind::Ndef], ]), }, })? .tag; } Ok(()) })write API 可用于将有效负载写入 NFC 标签。如果未扫描到标签且 keepSessionAlive: true 的NFC标签,应用将首先扫描 NFC 标签。
import { write, textRecord, uriRecord } from '@tauri-apps/plugin-nfc';
const payload = [uriRecord('https://tauri.app'), textRecord('some payload')];
const options = { // 仅在没有活跃的已扫描标签会话时才需要指定类型,其格式与提供给 scan() 方法的参数相同 kind: { type: 'ndef', }, // 配置 iOS 上“扫描 NFC”对话框中显示的信息 message: 'Scan a NFC tag', successfulReadMessage: 'NFC tag successfully scanned', successMessage: 'NFC tag successfully written',};
await write(payload, options);tauri::Builder::default() .setup(|app| { #[cfg(mobile)] { use tauri_plugin_nfc::NfcExt;
app.handle().plugin(tauri_plugin_nfc::init());
app .nfc() .write(vec![ tauri_plugin_nfc::NfcRecord { format: tauri_plugin_nfc::NFCTypeNameFormat::NfcWellKnown, kind: vec![0x55], // URI record id: vec![], payload: vec![], // insert payload here } ])?; } Ok(()) })默认情况下,所有潜在的危险插件命令和作用域均被阻止,无法访问。你必须修改 capabilities 配置中的权限才能启用这些功能。
有关更多信息,请参阅 功能概述, 关于如何使用插件权限,请参阅 分步指南。
{ "permissions": [ ..., "nfc:default", ]}Default Permission
This permission set configures what kind of operations are available from the nfc plugin.
Granted Permissions
Checking if the NFC functionality is available and scanning nearby tags is allowed. Writing to tags needs to be manually enabled.
This default permission set includes the following:
allow-is-availableallow-scan
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the is_available command without any pre-configured scope. |
|
|
Denies the is_available command without any pre-configured scope. |
|
|
Enables the scan command without any pre-configured scope. |
|
|
Denies the scan command without any pre-configured scope. |
|
|
Enables the write command without any pre-configured scope. |
|
|
Denies the write command without any pre-configured scope. |
© 2026 Tauri Contributors. CC-BY / MIT