在swift(IOS)中实现扫描仪NFC的问题

问题描述 投票:0回答:1

我有这个类来在我的应用程序中实现带有 NFC 的扫描仪。我看过不同的教程,很多人都使用这个类。它返回的错误:

类型“DniNFCViewController”不符合协议 'NFCNDEFReaderSessionDelegate'

此错误建议我添加新方法(在我的例子中为 readerSession),但它们已经实现了。然而,我已经在一个新项目中实现了这个类并且它工作正常。

//
//  DniNFCViewController.swift
//  SSLSignature
//
//  Created by Ismael Márquez on 20/2/24.
//  Copyright © 2024 aralink. All rights reserved.
//

import UIKit
import CoreNFC

class DniNFCViewController: UIViewController, NFCNDEFReaderSessionDelegate {
    
    var nfcSession: NFCNDEFReaderSession?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func startNFCSession(_ sender: Any) {
        nfcSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false)
        nfcSession?.begin()
    }
    
    // MARK: - NFCNDEFReaderSessionDelegate
    
    func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
        print("Session invalidated with error: \(error.localizedDescription)")
    }
    
    func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
        // Handle NFC tag detection here
        for message in messages {
            for record in message.records {
                // Process NDEF records
                // This is where you would handle the data read from the NFC tag
                print("Record payload: \(record.payload)")
            }
        }
    }
}

如何解决问题以及为什么会出现此错误?

ios swift nfc core-nfc
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.