2

I am creating a NativeScript plugin that wrap a Swift CocoaPod lib.

In my NativeScript plugin, I am using a separate class to add required delegate to main class.

Will it be possible to implement delegate methods in main class and avoid the delegate class completely?

i.e.,

export class MicrosoftBandService  extends NSObject implements ConnectionDelegate {

  static new(): MicrosoftBandService {
    return <MicrosoftBandService>super.new()
  }

  constructor() {
    let _self= this;
    this.mbk = MicrosoftBand.alloc().init();
    this.mbk.connectDelegate = _self
  }

  onConnecte() {
    //

  onDisconnecte() {
    //
  }

  onErrorWithError(error) {
    //
  }

}

even better, I like to do like this

export class MicrosoftBandService  extends MicrosoftBand implements ConnectionDelegate {

  static new(): MicrosoftBandService {
    return <MicrosoftBandService>super.new()
  }

  constructor() {
    let _self= this;
    this.connectDelegate = _self
  }

  onConnecte() {
    //
  }

    onDisconnecte() {
      //
    }

    onErrorWithError(error) {
      //
    }

  }

I don't know proper syntax to implement constructor with self delegate in TypeScript for NativeScript

My current code works, but I am looking for help to simplify and reduce code by eliminating separate delegate class.

1
  • 1
    I just want to add one comment; if you can avoid using Swift; you are better off doing so. Swift adds a bit over 20 megs of additional runtimes to the app. Commented Feb 7, 2017 at 5:12

1 Answer 1

3

When TypeScript is converted to Javascript interfaces are lost. So you need to use ObjCProtocols static field and declare which protocols are implemented by this class like:

export class MicrosoftBandService extends NSObject implements ConnectionDelegate {    
   public static ObjCProtocols = [ ConnectionDelegate ];
   ...
}

Second TypeScript constructors are not called on native objects (you probably have warning in the console about that). So you should do init method and set all fields there:

public static initWithOwner(band: MicrosoftBand): MicrosoftBandService {
    let delegate = <MicrosoftBandService>MicrosoftBandService.new();
    delegate._band = band; 
    band._delegate = delegate;
    band.connectDelegate = delegate.
}

There are few things to note here:

  1. Avoid new() overload. Could cause issues on some native object.

  2. Pass the band instance so that you could call it in the delegate methods (if you need it)

  3. Keep reference of the delegate locally on the band - most delegates are implemented as weak references. If you don't keep a reference to it no one will and it will be collected by the GC at some point.

More information about protocol implementation can be found here: http://docs.nativescript.org/runtimes/ios/how-to/ObjC-Subclassing

edit: It should be possible to do:

export class MicrosoftBandService extends MicrosoftBand implements ConnectionDelegate

as long as you add the static ObjCProtocols field. Then you won't need to keep a reference to the delegate because it will be the same instance. Still TypeScript constructor won't be called so you need to call the correct init native method.

Sign up to request clarification or add additional context in comments.

2 Comments

can you update full code for implement the delegate methods at run time.
I need your help for implement the run time delegate methods github.com/NativeScript/NativeScript/issues/6872

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.