Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Demo/NFC Read-Write/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"sourceLanguage" : "en",
"strings" : {
"Close Keyboard" : {
"extractionState" : "stale",
"localizations" : {
"zh-HK" : {
"stringUnit" : {
Expand Down Expand Up @@ -30,6 +31,9 @@
}
}
}
},
"SwiftNFC" : {

},
"Text" : {
"localizations" : {
Expand Down
1 change: 1 addition & 0 deletions Demo/NFC Read-Write/NFC_Read_WriteApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct NFC_Read_WriteApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.statusBarHidden(true)
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions Demo/NFC Read-Write/Views/DraggableInterfaceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ struct DraggableInterfaceView<TopContent: View, BottomContent: View, DragContent
let topContent: TopContent
let bottomContent: BottomContent
let dragContent: DragContent

@State private var topHeight: CGFloat = 0
@State private var isDragging = false
@State private var topSafeArea: CGFloat = 0

init(
@ViewBuilder topContent: () -> TopContent,
Expand All @@ -28,11 +29,12 @@ struct DraggableInterfaceView<TopContent: View, BottomContent: View, DragContent
var body: some View {
GeometryReader { geometry in
let initialHeight = geometry.size.height / 2

VStack(spacing: 0) {
// Top section
VStack {
topContent
.padding(.top, topSafeArea)
}
.frame(height: topHeight)
.frame(maxWidth: .infinity)
Expand Down Expand Up @@ -85,7 +87,13 @@ struct DraggableInterfaceView<TopContent: View, BottomContent: View, DragContent
}
.onAppear {
if topHeight == 0 {
topHeight = initialHeight - 100
topHeight = initialHeight
}
if topSafeArea == 0 {
topSafeArea = UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.first?.windows
.first?.safeAreaInsets.top ?? 0
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ func write() {
}
```

## Reactive Properties

Both `NFCReader` and `NFCWriter` classes use `@Published` properties for automatic SwiftUI view updates:

### NFCReader Properties
- `startAlert` - Alert message shown when scanning starts
- `endAlert` - Custom alert message after scanning (optional)
- `msg` - Decoded NFC message content (updates after scan)
- `raw` - Raw NFC data with type, identifier, and payload information

### NFCWriter Properties
- `startAlert` - Alert message shown when writing starts
- `endAlert` - Custom alert message after writing (optional)
- `msg` - Content to write to the NFC tag
- `type` - Record type: "T" for text (default) or "U" for URI

These properties automatically trigger view updates when changed, enabling seamless SwiftUI integration with two-way bindings:

```swift
// Example: Bind to a TextField
TextField("Message", text: $NFCW.msg)

// Example: Display scan results
Text(NFCR.msg)
```

## Demo
Path: `./Demo` (Xcode Project in SwiftUI)

Expand Down
19 changes: 9 additions & 10 deletions Sources/SwiftNFC/SwiftNFC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import CoreNFC
@available(iOS 15.0, *)
public class NFCReader: NSObject, ObservableObject, NFCNDEFReaderSessionDelegate {

public var startAlert = String(localized: "Hold your iPhone near the tag.", bundle: .module)
public var endAlert = ""
public var msg = String(localized: "Scan to read or Edit here to write...", bundle: .module)
public var raw = String(localized: "Raw Data available after scan.", bundle: .module)
@Published public var startAlert = String(localized: "Hold your iPhone near the tag.", bundle: .module)
@Published public var endAlert = ""
@Published public var msg = String(localized: "Scan to read or Edit here to write...", bundle: .module)
@Published public var raw = String(localized: "Raw Data available after scan.", bundle: .module)

public var session: NFCNDEFReaderSession?

Expand Down Expand Up @@ -50,12 +50,11 @@ public class NFCReader: NSObject, ObservableObject, NFCNDEFReaderSessionDelegate
}

public class NFCWriter: NSObject, ObservableObject, NFCNDEFReaderSessionDelegate {

public var startAlert = String(localized: "Hold your iPhone near the tag.", bundle: .module)
public var endAlert = ""
public var msg = ""
public var type = "T"

@Published public var startAlert = String(localized: "Hold your iPhone near the tag.", bundle: .module)
@Published public var endAlert = ""
@Published public var msg = ""
@Published public var type = "T"

public var session: NFCNDEFReaderSession?

public func write() {
Expand Down