0

I am creating a NativeScript plugin that will work with my iOS and Android SDKs. In my iOS code, I have an optional @protocol that contains a few optional methods with callbacks about the state of the SDK. I am now trying to access this protocol in NativeScript, and I tried searching the internet and following the guide here, but unfortunately, I still have issues with it. Here is a part of the @protocol in iOS:

@protocol BLASessionDelegate <NSObject>

@optional

- (void)sessionInitialized;

...

Here is the code I put in the "index.d.ts" in my plugin:

export interface SessionDelegate {
    sessionInitialized?();
    ...
}

Here is the part in the "bla.ios.d.ts" file in my plugin:

declare interface BLASessionDelegate extends NSObjectProtocol {
    sessionInitialized?();
}

@NativeClass()
declare class BLASessionDelegateImpl extends NSObject implements SessionDelegate {

    static ObjCProtocols = [BLASessionDelegate];

    static new(): BLASessionDelegateImpl {
        return <BLASessionDelegateImpl>super.new(); // calls new() on the NSObject
    }

    sessionDelegate: SessionDelegate;

    public sessionInitialized() {
        console.log("Adi - inside sessionInitialized");
        if(this.sessionDelegate.sessionInitialized !== undefined) {
            this.sessionDelegate.sessionInitialized();
        }
    }
}

Here is the part in the "bla.ios.ts" file in my plugin:

export const setSessionDelegate = function(sessionDelegate: SessionDelegate) {
    if(typeof(bla) !== "undefined") {
        console.log("Adi - in the setSessionDelegate");
        let delegate: BLASessionDelegateImpl = BLASessionDelegateImpl.new();
        delegate.sessionDelegate = sessionDelegate;
        bla.setSessionDelegate(delegate);
    }
}

This is the code I put in the test app in the "app.ts" file:

let delegate: bla.SessionDelegate = {
    sessionInitialized: function() {
        const sessionLink: string = bla.generateSessionLink();
        console.log("Adi - sessionLink = ", sessionLink);
    }
}

bla.setSessionDelegate(delegate);

I am still getting the following error with a crash when running the app and I can't figure out why:

***** Fatal JavaScript exception - application has been terminated. *****
  NativeScript encountered a fatal error: Uncaught ReferenceError: BLASessionDelegateImpl is not defined
  at
  setSessionDelegate(file: app/webpack:/myCoolApp/work/native-script-plugin/native-script/src/bla.ios.ts:165:41)
  at ./app/app.ts(file: app/webpack:/myCoolApp/app/app.ts:18:0)
  at __webpack_require__(file: app/webpack:/myCoolApp/webpack/bootstrap:24:0)
  at __webpack_exec__(file:///app/bundle.js:839:39)
  at (file:///app/bundle.js:840:318)
  at __webpack_require__.X(file: app/webpack:/myCoolApp/webpack/runtime/startup entrypoint:6:0)
  at (file:///app/bundle.js:840:47)
  at (file:///app/bundle.js:845:3)
  at require(:1:137)
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • try to move @NativeClass() class BLASessionDelegateImpl extends NSObject implements SessionDelegate { ... } before BLASessionDelegateImpl is used in "bla.ios.ts". – Yong Aug 31 '22 at 13:34
  • Thanks @Yong, this eliminated the error I had, but it presented an additional error - now the line "static ObjCProtocols = [BLASessionDelegate];" gives me the error "'BLASessionDelegate' only refers to a type, but is being used as a value here.ts(2693)". Do you have any idea what this means? In addition, the app now runs but unfortunately the "sessionInitialized" method inside the SDK does not get called and I don't get any result in the "generateSessionLink" part. Any idea on this too? – Adi Vizgan Sep 01 '22 at 10:25
  • It is difficult to figure it out without the source code. maybe your typings are not correct. You could have a look at Plugin ui-mapbox (https://github.com/nativescript-community/ui-mapbox/blob/19c1157734c2432a0da005124c1fb6ec7be5c3b8/src/ui-mapbox/typings/Mapbox.ios.d.ts#L1418) – Yong Sep 01 '22 at 12:44

0 Answers0