NFC
在 Android 和 iOS 上读取和写入 NFC 标签。
支持的平台
此插件需要 Rust 版本至少为 1.77.2
平台 | 级别 | 备注 |
---|---|---|
windows | | |
linux | | |
macos | | |
android | ||
ios |
设置
安装 nfc 插件以开始使用。
使用你的项目包管理器添加依赖项
npm run tauri add nfc
yarn run tauri add nfc
pnpm tauri add nfc
bun tauri add nfc
cargo 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
要在 iOS 上访问 NFC API,你必须在 Info.plist 文件中配置使用描述,并将 NFC 功能添加到你的应用程序。
Info.plist
在 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>
NFC 功能
此外,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
并非所有移动设备都具有扫描 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 标签
该插件可以扫描通用 NFC 标签或带有 NDEF(NFC 数据交换格式)消息的 NFC 标签,NDEF 是一种将类型化数据封装在 NFC 标签中的标准格式。
import { scan } from '@tauri-apps/plugin-nfc';
const scanType = { type: 'ndef', // or 'tag',};
const options = { keepSessionAlive: false, // configure the messages displayed in the "Scan NFC" dialog on iOS 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 = [ // capture anything using NfcF [TechKind.NfcF], // capture all MIFARE Classics with NDEF payloads [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(()) })
写入 NFC 标签
write
API 可用于将有效负载写入 NFC 标签。 如果没有使用 keepSessionAlive: true
扫描的标签,则应用程序将首先扫描 NFC 标签。
import { write, textRecord, uriRecord } from '@tauri-apps/plugin-nfc';
const payload = [uriRecord('https://tauri.org.cn'), textRecord('some payload')];
const options = { // the kind is only required if you do not have a scanned tag session alive // its format is the same as the argument provided to scan() kind: { type: 'ndef', }, // configure the messages displayed in the "Scan NFC" dialog on iOS 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", ]}
默认权限
此权限集配置了 nfc 插件可以执行的操作类型。
已授予的权限
允许检查 NFC 功能是否可用以及扫描附近的标签。 写入标签需要手动启用。
此默认权限集包括以下内容
allow-is-available
allow-scan
权限表
标识符 | 描述 |
---|---|
|
启用 is_available 命令,无需任何预配置的作用域。 |
|
拒绝 is_available 命令,无需任何预配置的作用域。 |
|
启用 scan 命令,无需任何预配置的作用域。 |
|
拒绝 scan 命令,无需任何预配置的作用域。 |
|
启用 write 命令,无需任何预配置的作用域。 |
|
拒绝 write 命令,无需任何预配置的作用域。 |
© 2025 Tauri 贡献者。CC-BY / MIT