NativeScript / ios-jsc

NativeScript for iOS using JavaScriptCore
http://docs.nativescript.org/runtimes/ios
Apache License 2.0
298 stars 59 forks source link

"Unrecognized selector sent to instance" when passing delegate to native library #1283

Closed jonathan-casarrubias closed 3 years ago

jonathan-casarrubias commented 3 years ago

Environment Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

Describe the bug Hi guys, I'm building a plugin using the GoogleWebRTC, most of the development has been flawless... But I'm having a problem I have searched for days on google, and still can't find the way to solve my issue:

So I created a Delegate implementation following your tutorials and documentation as follows:

export class RTCPeerConnectionDelegateImpl extends NSObject implements RTCPeerConnectionDelegate {

  public static ObjCProtocols = [RTCPeerConnectionDelegate];

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

  /** Many other methods in between that are unnecessary for this example **/

  peerConnectionShouldNegotiate(peerConnection: RTCPeerConnection): void {
    console.log("peerConnection should negotiate");
  }
}

But when I try to use this delegate:

    // Create Peer connection instance
    this.peerConnection = this.factory.peerConnectionWithConfigurationConstraintsDelegate(
      config,
      RTCMediaConstraints.alloc().initWithMandatoryConstraintsOptionalConstraints(null, new NSDictionary({
        objects: [kRTCMediaConstraintsValueTrue],
        forKeys: ['DtlsSrtpKeyAgreement']
      })),
      RTCPeerConnectionDelegateImpl.new() // <<<--- HERE
    );

I get this error

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RTCPeerConnectionDelegateImpl peerConnectionShouldNegotiate:]: unrecognized selector sent to instance 0x2808b0450'

So I understand the reason, the RTCPeerConnectionDelegateImpl is not providing the native peerConnectionShouldNegotiate selector when the delegated method is tried to be used by the WebRTC SDK, it won't find the method.

Then I tried to add the following to the class

  static ObjCExposedMethods = {
    "peerConnectionShouldNegotiate": { returns: interop.types.void, params: [ RTCPeerConnection ] }
  };

But I'm still getting the same error.

Since the delegate is optional if I remove the delegate parameter and I pass null instead, I successfully get a peerConnection, the problem is that there are some callbacks I need to listen now, and I'm unable until the delegate works, I think this is a pretty basic implementation, no really logic is in the middle, so either I'm missing a small detail or there is something off with delegates? might be related to IOS 14? I found several tickets of the "Unrecognized selector sent to instance" issue when people updates to IOS 14, but most issues are related to higher level problems, like they don't maintain the native module..

In my case I'm building at a lower level so none of their work arounds would work for me because, they were disabling some features until the plugin supports IOS 14, while I need this to make this work from the foundation.

Thanks

To Reproduce Just created a basic delegate and pass it to the library that will use it in this case passing a RTCPeerConnectionDelegate implementation to the GoogleWebRTC's peerConnection.delegate

Expected behavior Should trigger method peerConnectionShouldNegotiate