Skip to content

Commit

Permalink
Fix URI issue
Browse files Browse the repository at this point in the history
  • Loading branch information
1998code committed Apr 1, 2023
1 parent a687795 commit f39f346
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Demo/NFC Read-Write.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "NFC-Read-Write-Info.plist";
INFOPLIST_KEY_NFCReaderUsageDescription = "Use NFC to read to write data.";
INFOPLIST_KEY_NFCReaderUsageDescription = "Use NFC to read and write data.";
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES;
"INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;
Expand Down Expand Up @@ -337,7 +337,7 @@
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "NFC-Read-Write-Info.plist";
INFOPLIST_KEY_NFCReaderUsageDescription = "Use NFC to read to write data.";
INFOPLIST_KEY_NFCReaderUsageDescription = "Use NFC to read and write data.";
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES;
"INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;
Expand Down
9 changes: 8 additions & 1 deletion Demo/NFC Read-Write/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ struct ContentView: View {
}

// MARK: - Select NFC Option(s)
@AppStorage("type") private var type = "T"
var option: some View {
HStack {
Picker(selection: $NFCW.type, label: Text("Type Picker")) {
Picker(selection: $type, label: Text("Type Picker")) {
Text("Text").tag("T")
Text("Link").tag("U")
}
.onAppear {
NFCW.type = type
}
.onChange(of: type) { newType in
NFCW.type = newType
}
Spacer()
if keyboard {
Button(action: {
Expand Down
13 changes: 12 additions & 1 deletion Sources/SwiftNFC/SwiftNFC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ public class NFCWriter: NSObject, ObservableObject, NFCNDEFReaderSessionDelegate
session.alertMessage = "Read only tag detected."
session.invalidate()
case .readWrite:
let message = NFCNDEFMessage(records: [NFCNDEFPayload(format: .nfcWellKnown, type: Data("\(self.type)".utf8), identifier: Data(), payload: Data("\(self.msg)".utf8))])
let payload: NFCNDEFPayload?
if self.type == "T" {
payload = NFCNDEFPayload.init(
format: .nfcWellKnown,
type: Data("\(self.type)".utf8),
identifier: Data(),
payload: Data("\(self.msg)".utf8)
)
} else {
payload = NFCNDEFPayload.wellKnownTypeURIPayload(string: "\(self.msg)")
}
let message = NFCNDEFMessage(records: [payload].compactMap({ $0 }))
tag.writeNDEF(message, completionHandler: { (error: Error?) in
if nil != error {
session.alertMessage = "Write to tag fail: \(error!)"
Expand Down

0 comments on commit f39f346

Please sign in to comment.