跳转到内容
Tauri

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 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 文件中,添加以下代码片段:

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 文件添加以下配置来添加:

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();

该插件可以扫描通用 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);

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,
});

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);

默认情况下,所有潜在的危险插件命令和作用域均被阻止,无法访问。你必须修改 capabilities 配置中的权限才能启用这些功能。

有关更多信息,请参阅 功能概述, 关于如何使用插件权限,请参阅 分步指南

src-tauri/capabilities/default.json
{
"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-available
  • allow-scan

Permission Table

Identifier Description

nfc:allow-is-available

Enables the is_available command without any pre-configured scope.

nfc:deny-is-available

Denies the is_available command without any pre-configured scope.

nfc:allow-scan

Enables the scan command without any pre-configured scope.

nfc:deny-scan

Denies the scan command without any pre-configured scope.

nfc:allow-write

Enables the write command without any pre-configured scope.

nfc:deny-write

Denies the write command without any pre-configured scope.


© 2026 Tauri Contributors. CC-BY / MIT