iT邦幫忙

2024 iThome 鐵人賽

DAY 16
0
Mobile Development

手機Swift性能專家系列 第 16

NFC訊息建立以及寫入

  • 分享至 

  • xImage
  •  

首先我們建立一個訊息匡

  let newMessage = NFCNDEFMessage(records: [
            NFCNDEFPayload(format: .unchanged,
                           type: "com.example.payload12".data(using: .utf8)!,
                           identifier: "example-identifier12".data(using: .utf8)!,
                           payload: "訊息".data(use:.utf8)
        ])
        
        func writeToNFCTag(_ message: NFCNDEFMessage) async {
        guard let tag = detectedTag else {
            print("未檢測到 NFC 標籤")
            return
        }
        do {
            try await nfcManager.write(tag: tag, data: message)
        } catch {
            Alert.showAlert(title: "錯誤",
                            message: "寫入 NFC 標籤失敗:\(error.localizedDescription)",
                            vc: self,
                            confirmTitle: "確定") { [self] in 
                nfcManager.scanNFC()
            }
        }
    }
    
    func write(tag: NFCNDEFTag, data: NFCNDEFMessage) async throws {
        try await tag.writeNDEF(data)
    }
        ```
        然後資料帶入就可以寫入了

上一篇
CoreNFC程式實作
下一篇
swift 同態加密
系列文
手機Swift性能專家20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言