ios-jsc icon indicating copy to clipboard operation
ios-jsc copied to clipboard

WCSession "delegate SessionDelegate does not implement delegate method"

Open spbond opened this issue 8 years ago • 7 comments

let SessionDelegate = NSObject.extend({
    sessionActivationDidCompleteWithStateError : function ( session, activationState, error ) {
        console.log( "Session Activated" );
    },

    sessionDidBecomeInactive : function ( session ) {},
    sessionDidDeactivate     : function ( session ) {},

    sessionDidReceiveMessage : function ( session, message ) {
        console.log( "Message received: " + JSON.stringify( message, null, 4 ) );
    },

    sessionDidReceiveMessageReplyHandler : function ( session, message, replyHandler ) {
        console.log( "Message received: " + JSON.stringify( message, null, 4 ) );
        let reply = { "message" : "response" };
        replyHandler( reply ); // Return data to sending app
    }
},
{
    name: "SessionDelegate",
    protocols: [WCSessionDelegate]
});

I have been working on this problem for 2 days. I have scoured the Apple forums and documentation, as well as stack overflow and the nativscript documentation. When the watch app launches, it sends a message to the phone, but it is never delivered. Searching the logs reveals the error in the title. It also shows that WCSession is activated and has a delegate assigned. As far as I can tell, all of the delegate methods are declared properly, but the app can't seem to find them. It's also worth noting that sessionActivationDidCompleteWithStateError never seems to be called, even though the session is activated.

spbond avatar Sep 14 '17 00:09 spbond

@spbond The SessionDelegate methods are implemented in JavaScript but they are called from Objective-C, so you have to make them visible in the Objective-C world. You can do this by using the exposedMethods property. You can find an example of exposedMethods usage in the NativeScript doc.

ivanbuhov avatar Sep 14 '17 07:09 ivanbuhov

From your documentation, I was under the impression that when implementing a protocol, the exposed methods property was not necessary, and indeed, it is not needed to implement the app delegate. If there is something different in this case, it would be nice to know what it is, and you may also consider updating the documentation to clarify for future users.

spbond avatar Sep 14 '17 07:09 spbond

@spbond Оh, my fault, you are absolutely right. When the method is coming from an implemented protocol you don't have to add it in the exposedMethods array. It seems that you are doing everything right. You can try implementing the same behaviour in Objective-C to verify that the problem is happening only in NativeScript. Try to declare the protocol in native code as part of a plugin and consume it from JavaScript.

ivanbuhov avatar Sep 14 '17 10:09 ivanbuhov

I have no idea how I would go about adding an objective c plugin. I find the documentation vague and incomplete in this regard. On another note, I did some more digging and found that using console.log with application.ios.delegate after setting it to "AppDelegate" prints "function AppDelegate()", whereas logging WCSession.defaultSession().delegate after setting it to "SessionDelegate" prints "function NSObject()". If I try setting it to "AppDelegate" instead, it prints "function UIResponder()", so it appears to be interpreting its delegate as the base class. Is this expected behavior?

spbond avatar Sep 22 '17 05:09 spbond

Ok. I finally figured out how to create the plugin, and I have some new information. Communication still fails, seemingly at the same place, but this time the error is as follows:

-[WCSession onqueue_handleDictionaryMessageRequest:withPairingID:]_block_invoke delegate TNSDictionaryAdapter does not implement delegate method

I don't know what the TNSDictionaryAdapter is, but it appears to be a nativescript module.

spbond avatar Sep 26 '17 00:09 spbond

Ok, I found that there was a problem with my plugin because I didn't understand it very well. I have fixed it, and found that the delegate works when written in Objective-C. So it appears that this is either a problem with nativescript, or there is some detail that I am missing.

spbond avatar Sep 29 '17 20:09 spbond

Is this being looked into?

spbond avatar Oct 14 '17 22:10 spbond