Use the NFCService API
To develop a Lightning web component capable of interacting with NFCs, use the NFCService API.
- Import NFCService into your component definition to make the NFCService API functions available to your code.
- Test to make sure NFCService is available before you call NFC-related functions.
- Use the feature functions to perform NFC-related operations.
For complete API reference documentation of the NFCService API, see NFCService API in the Lightning Web Components Developer Guide.
In your component’s JavaScript file, import NFCService using the standard JavaScript import statement. Specifically, import the getNFCService() factory function from the lightning/mobileCapabilities module, like so:
After it’s imported into your component, use the factory function to get an instance of NFCService. With your NFCService instance, use the utility functions and constants to verify availability. Then use NFC-related functions to perform the associated functionality.
NFCService depends on physical device hardware and platform features. A component that uses NFCService renders without errors on a desktop computer or in a mobile browser, but NFC scanning functions fail. To avoid these errors, test if NFCService functionality is available before you use it.
Perform NFC read operations with read(). First, call the read() function, optionally passing in an NFCServiceOptions object as a parameter. Then, handle the result of the operation in any manner you wish.
For example:
Perform NFC erase operations with erase(). First, call the erase() function, optionally passing in an NFCServiceOptions object as a parameter. Then, handle the result of the operation in any manner you wish.
For example:
The erase() function can only be performed on writable NFC tags. Attempting this function on a read-only NFC tag results in an error.
Perform NFC write operations with write(). First, call the write() function, optionally passing in an NFCServiceOptions object as a parameter. Then, handle the result of the operation in any manner you wish.
For example:
The write() function can only be performed on writable NFC tags. Attempting this function on a read-only NFC tag will result in an error.