diff --git a/Demo/NFC Read-Write.xcodeproj/project.pbxproj b/Demo/NFC Read-Write.xcodeproj/project.pbxproj index a903845..66f95ed 100644 --- a/Demo/NFC Read-Write.xcodeproj/project.pbxproj +++ b/Demo/NFC Read-Write.xcodeproj/project.pbxproj @@ -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; @@ -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; diff --git a/Demo/NFC Read-Write/ContentView.swift b/Demo/NFC Read-Write/ContentView.swift index 8f1f60f..fd9f203 100644 --- a/Demo/NFC Read-Write/ContentView.swift +++ b/Demo/NFC Read-Write/ContentView.swift @@ -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: { diff --git a/Sources/SwiftNFC/SwiftNFC.swift b/Sources/SwiftNFC/SwiftNFC.swift index bf47efe..808d3fc 100644 --- a/Sources/SwiftNFC/SwiftNFC.swift +++ b/Sources/SwiftNFC/SwiftNFC.swift @@ -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!)"