NFC
Read and write NFC tags on Android and iOS.
Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | | |
linux | | |
macos | | |
android | ||
ios |
Setup
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 inCargo.toml
: -
Modify
lib.rs
to initialize the plugin: -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
Configuration
The NFC plugin requires native configuration for iOS.
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.
Info.plist
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:
NFC Capability
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:
Usage
The NFC plugin is available in both JavaScript and Rust, allowing you to scan and write to NFC tags.
Checking if NFC is supported
Not every mobile device has the capability to scan NFC tags, so you should check for availability before using the scan and write APIs.
Scanning NFC tags
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.
Filters
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.
Writing to NFC tags
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.
Permissions
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.
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.
allow-is-available
allow-scan
Permission Table
Identifier | Description |
---|---|
|
Enables the is_available command without any pre-configured scope. |
|
Denies the is_available command without any pre-configured scope. |
|
Enables the scan command without any pre-configured scope. |
|
Denies the scan command without any pre-configured scope. |
|
Enables the write command without any pre-configured scope. |
|
Denies the write command without any pre-configured scope. |
© 2024 Tauri Contributors. CC-BY / MIT