如何以编程方式在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标签需要做些什么。
答案 0 :(得分:11)
有四个步骤可以让它发挥作用:
实现委托并将其传递给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)
}
}