Corenfc NFcTag not able to write

45 Views Asked by At

We have imported CoreNFC and done with the implementation identifying UID and we are receiving iso7816 tag type but at the end of the result not able to write anything that should reflect in nfc business digital card we are receiving not connected tag error

   func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
            print("didDetect \(tags)")
            var nfcIso7816Tag: NFCISO7816Tag? = nil
            var nfcTag: NFCTag? = nil
    
            for tag in tags {
                if case let .iso7816(cTag) = tag {
                    nfcIso7816Tag = cTag
                    nfcTag = tag
                }
            }
    
            if nfcIso7816Tag == nil {
                session.invalidate(errorMessage: "Card not support")
            }
            let tag = tags.first!
            session.connect(to: nfcTag!) { (error) in
                if nil != error {
                    session.invalidate(errorMessage: "connection Failed")
                }
    //
                if case let .iso7816(sTag) = tag{
                    let  UID = sTag.identifier.map{String(format:"%.2hhx",$0)}.joined()
                    print("UID:",UID)
                    print(sTag.identifier)
                    session.alertMessage = "UID Captured"
                    let textPayload = NFCNDEFPayload.wellKnownTypeTextPayload(
                        string:"hello",
                        locale: Locale.init(identifier: "en")
    
                    )!
    
                    let uriPayloadFromURL = NFCNDEFPayload.wellKnownTypeURIPayload(
                        url: URL(string: "http://www.google.com")!
                    )!
    
                    let messge = NFCNDEFMessage.init(
                        records: [
                            uriPayloadFromURL,
                            textPayload
                        ]
                    )
                    sTag.queryNDEFStatus { status, _, error in
                        if let queryError = error {
                            print("Error querying NDEF status: \(queryError.localizedDescription)")
                            // Handle the error appropriately (e.g., invalidate session, show an error message)
                        } else {
                            if status == .readOnly {
                                print("Tag is read-only and does not support writing.")
                                // Handle accordingly, e.g., show a message to the user
                            } else if status == .readWrite {
                                // Continue with the writing logic
                                sTag.writeNDEF(messge) { writeError in
    
                                    if let writeError = writeError {
    
                                        session.invalidate(errorMessage: "Fail to write nfc card")
                                    } else {
                                        session.alertMessage = "Successfully writtern"
                                        session.invalidate()
                                    }
    
                                }
    
                            }
                        }
                    }
                }
            }

it is showing not able to connect tag

0

There are 0 best solutions below