作者:马先生2502911467 | 来源:互联网 | 2023-02-01 23:58
如何在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> Jens Meder..:
它有四个步骤可以使它工作:
将NFC标签权限添加到Apple开发人员门户中的应用标识符
将Code Signing Entitlement文件添加到项目并构建设置,并添加以下原始键和值:
将使用说明添加到Info.plist:
实现委托并将其传递给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)
}
}
}