我正在开发一个 iOS 应用程序,希望通过 NFC 扫描智能卡(例如借记卡或信用卡)并提取卡号、持卡人姓名、卡类型(例如 Visa、MasterCard)和到期日等信息。我了解 iOS 支持 NFC,但我不清楚可以从支持 NFC 的卡访问哪些类型的数据。有没有办法使用 Core NFC 等框架在 iOS 上安全地提取这些信息?
我正在通过下面的NFCTagReaderSession实现扫描NFC,但无法提取卡详细信息:
@IBAction func nfcClick(_ sender: Any) {
self.addHapticFeedback()
guard NFCTagReaderSession.readingAvailable else {
self.addErrorHapticFeedback()
let alert = UIAlertController(title: "Scanning Not Supported", message: "This device doesn't support tag scanning.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
self.session = NFCTagReaderSession(pollingOption: .iso14443, delegate: self)
self.session?.alertMessage = "Hold your iPhone 📱 Near the NFC Tag"
self.session?.begin()
}
extension DashboardVC: NFCTagReaderSessionDelegate {
func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) {
print("Session Activated.")
}
func tagReaderSession(_ session: NFCTagReaderSession, didInvalidateWithError error: any Error) {
print("Error with Launching Session and description is:", error.localizedDescription)
}
func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
print("Connecting To Tag")
if tags.count > 1 {
session.alertMessage = "More Than One Tag Detected, Please try again"
session.invalidate()
return
}
let tag = tags.first!
session.connect(to: tag) { (error) in
if let error = error {
session.invalidate(errorMessage: "Connection Failed: \(error.localizedDescription)")
return
}
print(tag, "*******")
switch tag {
case .miFare(let sTag):
let UID = sTag.identifier.map { String(format: "%.2hhx", $0)}.joined()
print("UID:", UID)
session.alertMessage = "UID Captured"
session.invalidate()
DispatchQueue.main.async {
print("Tag Detected")
}
case .iso15693(let sTag):
let UID = sTag.identifier.map { String(format: "%.2hhx", $0)}.joined()
print("ISO 15693 UID:", UID)
session.alertMessage = "ISO 15693 Tag Detected"
session.invalidate()
case .iso7816(let sTag):
print("ISO 7816 Tag Detected")
let UID = sTag.identifier.map { String(format: "%.2hhx", $0) }.joined()
print("ISO 7816 UID:", UID)
//Attempt to read ATR (Answer To Reset)
// let atr = sTag.atr.map { String(format: "%.2hhx", $0) }.joined()
// print("ISO 7816 ATR:", atr)
session.invalidate()
default:
session.invalidate(errorMessage: "Unsupported tag type.")
print("Unsupported tag type.")
}
}
}
}