2017-06-06 36 views
3

如何在Xcode中,9CoreNFC調試工作不

func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) { 
    //What I need to do here 
} 

func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) { 
    //What I need to do here 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let sessionReader = NFCNDEFReaderSession.init(delegate: self, queue: nil, invalidateAfterFirstRead: true) 
    let nfcSession = NFCReaderSession.self 

    let nfcTag = NFCTagCommandConfiguration.init() 
    let tagType = NFCTagType(rawValue: 0) 

    sessionReader.begin() 

} 

我想知道什麼,我需要做一些閱讀NFC標籤編程工作CoreNFC。

+0

在你不介意閱讀Objective-C代碼的情況下,這裏有一個預製的示例項目:https://github.com/x43x61x69/Core-NFC-Example –

回答

10

有四個步驟,以得到它的工作:

  1. 添加NFC標籤允許你的應用程序標識符在蘋果開發者門戶

enter image description here

  • 將代碼簽名授權文件添加到您的項目並構建設置並添加以下原始密鑰和值:
  • enter image description here enter image description here

  • 添加的使用描述你的Info.plist:
  • enter image description here

  • 實現代理並將其傳遞到NFCNDEFReaderSession init,如下所示:

    import UIKit 
    import CoreNFC 
    
    @UIApplicationMain 
    class AppDelegate: UIResponder, UIApplicationDelegate, NFCNDEFReaderSessionDelegate { 
    
    var window: UIWindow? 
    var session: NFCNDEFReaderSession? 
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
        session = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false) 
        self.session?.begin() 
        return true 
    } 
    
    
    func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) { 
        print(error) 
    } 
    
    func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) { 
        print(messages) 
    } 
    

    }