2017-06-10 48 views
0

我想從掃描使用的iOS推出了核心NFC框架我的NFC標籤(NXP NTAG213)的NDEF數據11核心NFC有效載荷錯誤

我成功地內讀取有效載荷標籤:

TNF=1, Payload Type=<54>, Payload ID=<>, Payload=<026a61e3 8193e382 93e381ab e381a1e3 81af0a> 

我想提取的有效載荷部分,所以這裏就是我試過:

print("payload.payload") 

但發生錯誤。

這裏是我的源代碼:

import UIKit 
import CoreNFC 

class ViewController: UIViewController, NFCNDEFReaderSessionDelegate{ 

var payloadData = "HaveNoData" 

func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) { 
    print("エラーは発生しませんでした") 
    for message in messages{ 
     for payload in message.records { 
      print (payload) 
      payloadData = String(describing: payload.payload) 
     } 
    } 
    print(payloadData) 
} 

func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) { 
    print("Error: \(error.localizedDescription)") 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    let session:NFCNDEFReaderSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true) 

    session.begin() 

} 

@IBAction func launch(_ sender: Any) { 
    let session:NFCNDEFReaderSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true) 
    session.begin() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


} 

回答

0

我不知道你是否能在payload.payload使用describing:這裏。但我相信從NSData的老式String將工作。 (不是每天的Swift編碼器,所以不知道現在Swift 3/4如何處理這個問題)。

在Objective-C,我可以做到這一點:

NSString *dataString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 

或在您的情況NSUTF8StringEncoding爲日本的字符串。

Here是我做的一個樣例項目(在Objective-C中)。