Search code examples
nativescriptnativescript-angular

delegate callbacks are not raised with ios plugin


I'm building an ios plugin for a native library.

The issue I am facing is the library starts to work and the view controller presenter is working fine, but the callbacks which are passed through a delegate are never raised in typescript.

Here is the code snippet:

import { Common } from './nsjumioplugin.common';
import * as frameModule from "tns-core-modules/ui/frame/frame";


export class Nsjumioplugin extends Common {
    private cancelWithError;
    private finishInitWithError;
    private finishedScan;

    private netverifyViewController: NetverifyViewController;

    constructor() {
        super();
        this.netverifyViewController = NetverifyViewController.alloc();
    }


    public start(merchantApiToken, merchantApiSecret, customerEmail, cancelWithError, finishInitWithError, finishedScan) {
        this.cancelWithError = cancelWithError;
        this.finishInitWithError = finishInitWithError;
        this.finishedScan = finishedScan;

        console.log("EEEEEEEEEEEE greet 8");

        const that = this;
        const config = NetverifyConfiguration.new();
        config.merchantApiToken = merchantApiToken;
        config.merchantApiSecret = merchantApiSecret;
        config.delegate = NsjumiopluginDelegateImpl.new().initWithCallback((netverifyViewController: NetverifyViewController, documentData: NetverifyDocumentData, scanReference: string) => {
            console.log("EEEEEEEEEEE 4");
            that.rootVC().dismissViewControllerAnimatedCompletion(true, null);
            that.finishedScan(documentData);
        });
        config.customerId = customerEmail;

        try {
            this.netverifyViewController.initWithConfiguration(config);

            this.rootVC().presentViewControllerAnimatedCompletion(
                this.netverifyViewController, false, function completion() {
                console.log('EEEEEEEEEEEE done');
            });
        } catch (e) {
            console.log('EEEEEEEEEEEE EXCEPTION HANDLED:', e);
        }
    }

    rootVC() {
        /*
        const appWindow = UIApplication.sharedApplication.keyWindow;
        return appWindow.rootViewController;
        */

        let frame: typeof frameModule = require("tns-core-modules/ui/frame");

        let topMostFrame = frame.topmost();
        if (topMostFrame) {
           let viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;
           if (viewController) {
               while (viewController.parentViewController) {
                   // find top-most view controler
                   viewController = viewController.parentViewController;
               }

               while (viewController.presentedViewController) {
                   // find last presented modal
                   viewController = viewController.presentedViewController;
               }

               return viewController;
           }
        }

    }

}


class NsjumiopluginDelegateImpl extends NSObject implements NetverifyViewControllerDelegate {

    static new(): NsjumiopluginDelegateImpl {
      return <NsjumiopluginDelegateImpl>super.new();
    }
    private _callback: (netverifyViewController: NetverifyViewController, documentData: NetverifyDocumentData, scanReference: string) => void;

    public initWithCallback(callback: (netverifyViewController: NetverifyViewController, documentData: NetverifyDocumentData, scanReference: string) => void): NsjumiopluginDelegateImpl {
        this._callback = callback;
        return this;
    }

    netverifyViewControllerDidCancelWithErrorScanReference(netverifyViewController: NetverifyViewController, error: NetverifyError, scanReference: string): void {
        console.log("EEEEEEEEEEE 1");
        this._callback(netverifyViewController, null, scanReference);
    }

    netverifyViewControllerDidFinishInitializingWithError?(netverifyViewController: NetverifyViewController, error: NetverifyError): void {
        console.log("EEEEEEEEEEE 2");
        // this.finishInitWithError();
    }

    netverifyViewControllerDidFinishWithDocumentDataScanReference(netverifyViewController: NetverifyViewController, documentData: NetverifyDocumentData, scanReference: string): void {
        console.log("EEEEEEEEEEE 3");
        console.log("finished successfully with scan reference: %@", scanReference);
        this._callback(netverifyViewController, documentData, scanReference);
    }
  }

The only console logs I am getting are "EEEEEEEEEEEE greet 8" and "EEEEEEEEEEEE done". The method in delegate are never raised.

Here is the SDK docs: https://github.com/Jumio/mobile-sdk-ios

I am wondering which part I am doing wrong.

Thanks


Solution

  • You have to let the runtime know you are using particular delegate by add @ObjCClass(...) annotation.

    @ObjCClass(NetverifyViewControllerDelegate)
    class NsjumiopluginDelegateImpl extends NSObject implements NetverifyViewControllerDelegate {
     .....
    }
    

    An alternative syntax is to define static ObjCProtocols array,

    class NsjumiopluginDelegateImpl extends NSObject implements NetverifyViewControllerDelegate {
     public static ObjCProtocols = [NetverifyViewControllerDelegate];
     .....
    }