NFC
在 Android 和 iOS 上读写 NFC 标签。
此插件需要 Rust 版本至少为 **1.77.2**
| 平台 | 级别 | 备注 | 
|---|---|---|
| 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 访客绑定 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 标签,这是一种将类型数据封装在 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(())  })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