跳到内容
Tauri

NFC

在 Android 和 iOS 上读取和写入 NFC 标签。

支持的平台

此插件需要 Rust 版本至少为 1.77.2

平台 级别 备注
windows
linux
macos
android
ios

设置

安装 nfc 插件以开始使用。

使用你的项目包管理器添加依赖项

npm run tauri add nfc

配置

NFC 插件需要 iOS 的原生配置。

iOS

要在 iOS 上访问 NFC API,你必须在 Info.plist 文件中配置使用描述,并将 NFC 功能添加到你的应用程序。

Info.plist

在 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>

NFC 功能

此外,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

并非所有移动设备都具有扫描 NFC 标签的功能,因此在使用扫描和写入 API 之前,应检查其可用性。

import { isAvailable } from '@tauri-apps/plugin-nfc';
const canScanNfc = await isAvailable();

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

过滤器

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

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

权限

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

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

src-tauri/capabilities/default.json
{
"permissions": [
...,
"nfc:default",
]
}

默认权限

此权限集配置了 nfc 插件可以执行的操作类型。

已授予的权限

允许检查 NFC 功能是否可用以及扫描附近的标签。 写入标签需要手动启用。

此默认权限集包括以下内容

  • allow-is-available
  • allow-scan

权限表

标识符 描述

nfc:allow-is-available

启用 is_available 命令,无需任何预配置的作用域。

nfc:deny-is-available

拒绝 is_available 命令,无需任何预配置的作用域。

nfc:allow-scan

启用 scan 命令,无需任何预配置的作用域。

nfc:deny-scan

拒绝 scan 命令,无需任何预配置的作用域。

nfc:allow-write

启用 write 命令,无需任何预配置的作用域。

nfc:deny-write

拒绝 write 命令,无需任何预配置的作用域。


© 2025 Tauri 贡献者。CC-BY / MIT