SwiftNFCClosureHandler is a Swift package for scanning NFC tags on iOS for your SwiftUI projects.
The scanned NFC data is returned as a Swift Data via closure. You can then massage the data to your specific needs.
Once you import the Swift package to your project, this is what you can do:
import SwiftNFCClosureHandler
struct ContentView: View {
// Declare a @StateObject for the SwiftNFCClosureHandler
@StateObject var nfc: SwiftNFCClosureHandler = SwiftNFCClosureHandler()
// Your button code
...
Button(action: {
nfc.begin(action: { data in
if let string = String(data: data, encoding: .ascii) {
self.nfc_value = string
}
})
}) {
Text("Click to Scan")
}
...
}
Here is the step-by-step guide at getting your SwiftUI project running with SwiftNFCClosureHandler:
- Open the
File
menu at XCode, then selectAdd the packages
. Pasted the URL https://github.com/pulsely/SwiftNFCClosureHandler at the search box.
- Then, click the project name at the Project Panel, and select
Signing & Capabilities
. Add theNear Field Communication Tag Reading
capability to your project.
- For your project target, select
Info
, and add thePrivacy - NFC Scan Usage Description
as a string property with your privacy message of choice.
-
Now, make sure you have linked
CoreNFC.framework
at theBuild Phases
of your target. -
Add your own SwiftUI code. Copy the code at the
How to
above or use code of the example project
Sample project for using the SwiftNFCClosureHandler is available at SwiftNFCClosureHandler-example.
The SwiftNFCClosureHandler Swift package is licenced under the MIT License.