Search code examples
iosobjective-ciphonexcodecore-nfc

iOS 11 CoreNFC How Does One Handle Reading Errors?


CoreNFC has a delegate method for errors being:

//Called when the NFC session invalidates with an error.
- (void)readerSession:(nonnull NFCNDEFReaderSession *)session didInvalidateWithError:(nonnull NSError *)error {
}

The documentation (https://developer.apple.com/documentation/corenfc) shows on the error section (https://developer.apple.com/documentation/corenfc/nfcreadererror) a bunch of error codes.

I want to be able to read the error that came from the reader session and put it inside of a switch statement that I can output a different message per error. I can't figure out how to get those error message from the function. I'm assuming I missed something on basic objective c on casting on something.

What I hope to get is something like this.

switch (error) {
        case NFCReaderErrorSecurityViolation:
            //Do Stuff
            break;
        case NFCReaderErrorUnsupportedFeature:
            //NFC is unsupported.
            break;
        //ETC
        default:
            break;
    }

How do I get that?


Solution

  • Use error.code in the switch block as follows,

    switch (error.code) {
        case NFCReaderErrorSecurityViolation:
            //Do Stuff
            break;
        case NFCReaderErrorUnsupportedFeature:
            //NFC is unsupported.
            break;
        //ETC
        default:
            break;
    }