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)