Search code examples
ioscordovaionic-frameworknfcphonegap

Ionic, phonegap nfc Exception in iOS


I am developing an Ionic app which reads nfc tags. But when I am running it in iOS I am getting the result from the nfc tag, but after that I am getting NSExcetion.

Here is the part of the code where I am trying to read NFC Tag:

if(this.platform.is("ios")) {
      this.nfc.beginSession().subscribe(() => {
        this.nfc.addNdefListener((data) => {
          console.log("IOS: ",data) // You will not see this, at this point the app will crash
        })
      });  
  }

I never get the console.log !!!!!

These are the errors that I am getting in Xcode (version 9.2):

2018-02-01 09:06:57.987510-0500 androidSDK[1819:852595]   readerSessionDidBecomeActive
2018-02-01 09:07:03.035369-0500 androidSDK[1819:852705] NFCNDEFReaderSession didDetectNDEFs
2018-02-01 09:07:03.035641-0500 androidSDK[1819:852705] {"ndefMessage":[{"tnf":1,"id":[],"payload":[2,101,110,72,101,108,108,111,46,33],"type":[84]},{"tnf":1,"id":[],"payload":[2,101,110,72,101,108,108,111,32,119,111,114,108,100,33,32],"type":[84]}]}
2018-02-01 09:07:03.035975-0500 androidSDK[1819:852430] -[WKWebView stringByEvaluatingJavaScriptFromString:]: unrecognized selector sent to instance 0x15e046200
2018-02-01 09:07:03.036619-0500 androidSDK[1819:852430] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WKWebView stringByEvaluatingJavaScriptFromString:]: unrecognized selector sent to instance 0x15e046200'
*** First throw call stack:
(0x18121b164 0x180464528 0x181228628 0x18ab933c8 0x181220b10 0x181105ccc 0x104b74880 0x105cfd2cc 0x105cfd28c 0x105d01ea0 0x1811c3344 0x1811c0f20 0x1810e0c58 0x182f8af84 0x18a836804 0x104b2b1cc 0x180c0056c)
libc++abi.dylib: terminating with uncaught exception of type NSException
2018-02-01 09:07:03.036798-0500 androidSDK[1819:852705] didInvalidateWithError Single tag read (null)
  • Ionic version: 3.19.1
  • iOS platform version: 4.5.4
  • Cordova version: 8.0.0
  • Phonegap-nfc plugin: phonegap-nfc 0.7.1 "NFC"

Solution

  • The problem is the plugin, it just works with UIWebView, but you are using WKWebView, so this line fails

    [(UIWebView*)[self webView] stringByEvaluatingJavaScriptFromString: function];
    

    There is a pull request available to fix this problem, so click on the thumbs up button so maybe it's taken into account and merged, or use the fork instead.