Read and write NFC tags on Android and iOS.
This plugin requires a Rust version of at least 1.77.2
Platform Level Notes windows
linux
macos
android
ios
Install the nfc plugin to get started.
Use your project’s package manager to add the dependency:
Run the following command in the src-tauri
folder to add the plugin to the project’s dependencies in Cargo.toml
:
cargo add tauri-plugin-nfc --target ' cfg(any(target_os = "android", target_os = "ios")) '
Modify lib.rs
to initialize the plugin:
#[cfg_attr(mobile, tauri :: mobile_entry_point)]
tauri :: Builder :: default ()
app . handle () . plugin (tauri_plugin_nfc :: init ());
. run (tauri :: generate_context! ())
. expect ( " error while running tauri application " );
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
npm install @tauri-apps/plugin-nfc
yarn add @tauri-apps/plugin-nfc
pnpm add @tauri-apps/plugin-nfc
deno add npm:@tauri-apps/plugin-nfc
bun add @tauri-apps/plugin-nfc
The NFC plugin requires native configuration for iOS.
To access the NFC APIs on iOS you must configure a usage description on the Info.plist file and add the NFC capability to your application.
On iOS the NFC plugin requires the NFCReaderUsageDescription
information property list value, which should describe why your app needs to scan or write to NFC tags.
In the src-tauri/Info.ios.plist
file, add the following snippet:
<? 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" >
< key > NFCReaderUsageDescription </ key >
< string > Read and write various NFC tags </ string >
Additionally iOS requires the NFC capability to be associated with your application.
The capability can be added in Xcode in the project configuration’s “Signing & Capabilities” tab by clicking the ”+ Capability” button and
selecting the “Near Field Communication Tag Reading” capability (see Add a capability to a target for more information)
or by adding the following configuration to the gen/apple/<app-name>_iOS/<app-name>_iOS.entitlements
file:
<? 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" >
< key > com.apple.developer.nfc.readersession.formats </ key >
The NFC plugin is available in both JavaScript and Rust, allowing you to scan and write to NFC tags.
Not every mobile device has the capability to scan NFC tags, so you should check for availability before using the scan and write APIs.
import { isAvailable } from ' @tauri-apps/plugin-nfc ' ;
const canScanNfc = await isAvailable ();
tauri :: Builder :: default ()
use tauri_plugin_nfc :: NfcExt;
app . handle () . plugin (tauri_plugin_nfc :: init ());
let can_scan_nfc = app . nfc () . is_available () ? ;
The plugin can scan either generic NFC tags or NFC tags with a NDEF (NFC Data Exchange Format) message,
which is a standard format to encapsulate typed data in an NFC tag.
import { scan } from ' @tauri-apps/plugin-nfc ' ;
type: ' ndef ' , // or 'tag',
// 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 ()
use tauri_plugin_nfc :: NfcExt;
app . handle () . plugin (tauri_plugin_nfc :: init ());
. scan (tauri_plugin_nfc :: ScanRequest {
kind : tauri_plugin_nfc :: ScanKind :: Ndef {
keep_session_alive : false ,
Note
The keepSessionAlive
option can be used to directly write to the scanned NFC tag later.
If you do not provide that option, the session is recreated on the next write()
call,
which means the app will try to rescan the tag.
The NFC scanner can also filter tags with a specific URI format, mime type or NFC tag technologies.
In this case, the scan will only detect tags that matches the provided filters.
Note
Filtering is only available on Android, so you should always check the scanned NFC tag contents.
The mime type is case sensitive and must be provided with lower case letters.
import { scan, TechKind } from ' @tauri-apps/plugin-nfc ' ;
// capture anything using NfcF
// capture all MIFARE Classics with NDEF payloads
[ TechKind . NfcA , TechKind . MifareClassic , TechKind . Ndef ],
type: ' ndef ' , // or 'tag'
tauri :: Builder :: default ()
use tauri_plugin_nfc :: NfcExt;
app . handle () . plugin (tauri_plugin_nfc :: init ());
. 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 ()),
vec! [tauri_plugin_nfc :: TechKind :: Ndef],
The write
API can be used to write a payload to a NFC tag.
If there’s no scanned tag with keepSessionAlive: true
, the application will first scan an NFC tag.
import { write, textRecord, uriRecord } from ' @tauri-apps/plugin-nfc ' ;
const payload = [ uriRecord ( ' https://tauri.app ' ), textRecord ( ' some payload ' )];
// 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()
// 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 ()
use tauri_plugin_nfc :: NfcExt;
app . handle () . plugin (tauri_plugin_nfc :: init ());
tauri_plugin_nfc :: NfcRecord {
format : tauri_plugin_nfc :: NFCTypeNameFormat :: NfcWellKnown,
kind : vec! [ 0x55 ], // URI record
payload : vec! [], // insert payload here
By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities
configuration to enable these.
See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
This permission set configures what kind of
operations are available from the nfc plugin.
Checking if the NFC functionality is available
and scanning nearby tags is allowed.
Writing to tags needs to be manually enabled.
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.
© 2024 Tauri Contributors. CC-BY / MIT