CoreNFC调试无法正常工作

时间:2017-06-06 12:39:08

标签: ios ios11 xcode9-beta

如何以编程方式在xcode-9上运行CoreNFC

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标签需要做些什么。

1 个答案:

答案 0 :(得分:11)

有四个步骤可以让它发挥作用:

  1. 将NFC标签权限添加到Apple开发人员门户网站中的应用标识符
  2. enter image description here

    1. 将代码签名授权文件添加到项目并构建设置,并添加以下原始密钥和值:
    2. enter image description here enter image description here

      1. 将使用说明添加到Info.plist:
      2. enter image description here

        1. 实现委托并将其传递给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)
          }
          

          }