Skip to content

Commit f39f346

Browse files
committed
Fix URI issue
1 parent a687795 commit f39f346

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

Demo/NFC Read-Write.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@
297297
ENABLE_PREVIEWS = YES;
298298
GENERATE_INFOPLIST_FILE = YES;
299299
INFOPLIST_FILE = "NFC-Read-Write-Info.plist";
300-
INFOPLIST_KEY_NFCReaderUsageDescription = "Use NFC to read to write data.";
300+
INFOPLIST_KEY_NFCReaderUsageDescription = "Use NFC to read and write data.";
301301
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
302302
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES;
303303
"INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;
@@ -337,7 +337,7 @@
337337
ENABLE_PREVIEWS = YES;
338338
GENERATE_INFOPLIST_FILE = YES;
339339
INFOPLIST_FILE = "NFC-Read-Write-Info.plist";
340-
INFOPLIST_KEY_NFCReaderUsageDescription = "Use NFC to read to write data.";
340+
INFOPLIST_KEY_NFCReaderUsageDescription = "Use NFC to read and write data.";
341341
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
342342
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES;
343343
"INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;

Demo/NFC Read-Write/ContentView.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,19 @@ struct ContentView: View {
6565
}
6666

6767
// MARK: - Select NFC Option(s)
68+
@AppStorage("type") private var type = "T"
6869
var option: some View {
6970
HStack {
70-
Picker(selection: $NFCW.type, label: Text("Type Picker")) {
71+
Picker(selection: $type, label: Text("Type Picker")) {
7172
Text("Text").tag("T")
7273
Text("Link").tag("U")
7374
}
75+
.onAppear {
76+
NFCW.type = type
77+
}
78+
.onChange(of: type) { newType in
79+
NFCW.type = newType
80+
}
7481
Spacer()
7582
if keyboard {
7683
Button(action: {

Sources/SwiftNFC/SwiftNFC.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,18 @@ public class NFCWriter: NSObject, ObservableObject, NFCNDEFReaderSessionDelegate
104104
session.alertMessage = "Read only tag detected."
105105
session.invalidate()
106106
case .readWrite:
107-
let message = NFCNDEFMessage(records: [NFCNDEFPayload(format: .nfcWellKnown, type: Data("\(self.type)".utf8), identifier: Data(), payload: Data("\(self.msg)".utf8))])
107+
let payload: NFCNDEFPayload?
108+
if self.type == "T" {
109+
payload = NFCNDEFPayload.init(
110+
format: .nfcWellKnown,
111+
type: Data("\(self.type)".utf8),
112+
identifier: Data(),
113+
payload: Data("\(self.msg)".utf8)
114+
)
115+
} else {
116+
payload = NFCNDEFPayload.wellKnownTypeURIPayload(string: "\(self.msg)")
117+
}
118+
let message = NFCNDEFMessage(records: [payload].compactMap({ $0 }))
108119
tag.writeNDEF(message, completionHandler: { (error: Error?) in
109120
if nil != error {
110121
session.alertMessage = "Write to tag fail: \(error!)"

0 commit comments

Comments
 (0)