iosflutterinfo.plistcore-nfc

Flutter NFC: "The connection to service named com.apple.nfcd.service.corenfc was invalidated from this process"


when I run in android, it run smoothly but when I run in the IOS device IOS 14.6, I got this error

XPC Error: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.nfcd.service.corenfc was invalidated from this process." UserInfo={NSDebugDescription=The connection to service named com.apple.nfcd.service.corenfc was invalidated from this process.}

The code:

@override
  void initState() {
    super.initState();

    _registerNFC();
  }

void _registerNFC() async {
    NfcManager.instance.startSession(
        pollingOptions: {
          NfcPollingOption.iso14443,
          NfcPollingOption.iso15693,
          NfcPollingOption.iso18092,
        },
        onDiscovered: (NfcTag tag) async {
          print(tag.toString());
          String mytag = "";

          if (tag.data.containsKey("mifareclassic")) {
            mytag = tag.data["mifareclassic"]["identifier"]
                .map((e) => e.toRadixString(16).padLeft(2, '0'))
                .join('')
                .toString()
                .toUpperCase();

            print("classic");
            
          } else if (tag.data.containsKey("mifareultralight")) {
            mytag = tag.data["mifareultralight"]["identifier"]
                .map((e) => e.toRadixString(16).padLeft(2, '0'))
                .join('')
                .toString()
                .toUpperCase();

            print("ultralight");
          } else if (tag.data.containsKey("mifareplus")) {
            mytag = tag.data["mifareplus"]["identifier"]
                .map((e) => e.toRadixString(16).padLeft(2, '0'))
                .join('')
                .toString()
                .toUpperCase();

            print("plus");
          } else if (tag.data.containsKey("mifaredesfire")) {
            mytag = tag.data["mifaredesfire"]["identifier"]
                .map((e) => e.toRadixString(16).padLeft(2, '0'))
                .join('')
                .toString()
                .toUpperCase();

            print("des fire");
          } else {
            mytag = 'unknown type card';
          }

          // var lastID = mytag[mytag.length - 1];

          result.value = mytag;
          uid = result.value;
          NfcManager.instance.stopSession();
          print("close session");
          _updateNFC();


          try {
            // await ndef.write(message);
            result.value = 'Success to "Ndef Write"';
            NfcManager.instance.stopSession();

            //_ndefWriteLock(); jangan letak kalau tak nnt tak boleh unlock balik
          } catch (e) {
            result.value = e;
            NfcManager.instance
                .stopSession(errorMessage: result.value.toString());
            return;
          }
        });
  }

  void _updateNFC() {
    if (uid != 'unknown type card') {
      Navigator.pop(context, uid);
    } else {
      CustomDialog.show(context,
          isDissmissable: false,
          title: "Unknown NFC tag",
          description:
              "Please use another type. Type allowed: [MifareClassic] [MifareUltralight] [MifareDesFire]",
          btnOkText: "I Understand", btnOkOnPress: () {
        Navigator.pop(context);
        print("open session");
        _registerNFC();
      });
    }
  }

info.plist file

<key>NFCReaderUsageDescription</key>
        <string>to scan the NFC Tag on the fire extinguisher</string>

<key>UIRequiredDeviceCapabilities</key>
        <array>
            <string>nfc</string>
        </array>
   <key>com.apple.developer.nfc.readersession.felica.systemcodes</key>
        <array>
            <string>0003</string>
            <string>04D1</string>
            <string>8008</string>
            <string>80DE</string>
            <string>865E</string>
            <string>8592</string>
            <string>8B5D</string>
            <string>8FC1</string>
            <string>FE00</string>
        </array>

        <key>com.apple.developer.nfc.readersession.formats</key>
        <array>
            <string>TAG</string>
            <string>NDEF</string>
        </array>
        <key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key>
        <array>
            <string>A0000002310100000000000000000000</string>
            <string>A0000002310200000000000000000000</string>
            <string>A0000002480300000000000000000000</string>
           
        </array>

anyone know how to setup? I've also use other package like flutter_nfc_kit but still got the same error


Solution

  • You have to add the Near Field Communication Tag Reader Session Formats Entitlement.

    I just tried to run your code without adding it, and basically got the same error message:

    Error: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.nfcd.service.corenfc was invalidated: failed at lookup with error 159 - Sandbox restriction." UserInfo={NSDebugDescription=The connection to service named com.apple.nfcd.service.corenfc was invalidated: failed at lookup with
    

    Open your Runner.xcodeproj and click on:

    enter image description here

    You can also see 2:55 of this YouTube video to see how to add it.

    You should now stop and rebuild the project, and should have a working NFC app:

    enter image description here