Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communication between webviews #8

Open
cvuser0 opened this issue Dec 7, 2015 · 4 comments
Open

Communication between webviews #8

cvuser0 opened this issue Dec 7, 2015 · 4 comments

Comments

@cvuser0
Copy link
Contributor

cvuser0 commented Dec 7, 2015

I haven't seen one, but is there a possibility of communication between the webviews?
The inappbrowser plugin has a form of limited communication but this plugin suits me better except the cross-webview communication.

@arturokunder
Copy link
Contributor

@cvuser0 We implemented a callback that is triggered when the second webview is closed.
See #9
You can use this as an example for making a wider range for communication between other webviews,

@cvuser0
Copy link
Contributor Author

cvuser0 commented Dec 9, 2015

@arturokunder that callback is triggered by the WebViewPlugin.java file, i got to the point of communicating with that file.
I am currently struggling with getting hold of the WebViewActivity and calling functions there (and calling WebViewPlugin's functions from WebViewActivity).

@AshMartian
Copy link
Contributor

This would be great, currently I'm working around this by using sqlite.
https://github.com/litehelpers/Cordova-sqlite-storage

Reading in second window, writing on the original.

@AshMartian
Copy link
Contributor

AshMartian commented Oct 4, 2017

I attempted to implement this functionality in a fork, no success yet. I can call the events on the same webview, but they are using two different native objects hence won't share the same dictionary (iOS).
Here's what I tried (added)

webViewPlugin.js

var _sendEvent = function(event, successCallback, errorCallback) {
    cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'sendEvent', [event]);
  };

  var _subscribeEvent = function(event, successCallback, errorCallback) {
    cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'subscribeEvent', [event]);
  };

  var _unsubscribeEvent = function(event, successCallback, errorCallback) {
    cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'unsubscribeEvent', [event]);
  };

WebViewPlugin.h

@interface WebViewPlugin : CDVPlugin<WebViewDelegate>
{
  @private NSString* webViewFinishedCallBack;
  @private NSMutableDictionary* webViewEventCallbacks;
}
@property (nonatomic, retain) WebViewController* webViewController;

- (void)subscribeCallback:(CDVInvokedUrlCommand*)command;
- (void)unsubscribeEvent:(CDVInvokedUrlCommand*)command;
- (void)subscribeEvent:(CDVInvokedUrlCommand*)command;
- (void)sendEvent:(CDVInvokedUrlCommand*)command;
- (void)show:(CDVInvokedUrlCommand*)command;
- (void)hide:(CDVInvokedUrlCommand*)command;
- (void)exitApp:(CDVInvokedUrlCommand*)command;
- (void)webViewFinished;

WebViewPlugin.m


- (void)subscribeEvent:(CDVInvokedUrlCommand*)command
{
    NSString* event=(NSString*)[command.arguments objectAtIndex:0];
    @try {
        [webViewEventCallbacks setObject:command.callbackId forKey:event];
    }
    @catch (NSException *exception) {
        NSString* reason=[exception reason];
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: reason];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
    NSLog(@"Subscribing to Event %@", event);
}

- (void)unsubscribeEvent:(CDVInvokedUrlCommand*)command
{
    NSString* event=(NSString*)[command.arguments objectAtIndex:0];
    @try {
        [webViewEventCallbacks removeObjectForKey:event];
    }
    @catch (NSException *exception) {
        NSString* reason=[exception reason];
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: reason];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
    NSLog(@"Unsubscribing to Event %@", event);
}

- (void)sendEvent:(CDVInvokedUrlCommand*)command
{
    NSString* event=(NSString*)[command.arguments objectAtIndex:0];
    NSString* message= @"";
    if([command.arguments count] > 1) {
        message = (NSString*)[command.arguments objectAtIndex:1];
    }
    NSLog(@"Sending to Event %@", event);
    @try {
        [self.commandDelegate runInBackground:^{
            NSString *_subscribeCallbackId = [webViewEventCallbacks objectForKey: event];
            NSLog(@"Got subscribe ID %@", webViewEventCallbacks);
            CDVPluginResult *pluginResult = nil;
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: message];
            [pluginResult setKeepCallbackAsBool:TRUE];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:_subscribeCallbackId];
        }];
        
    } @catch(NSException *exception) {
        NSString* reason=[exception reason];
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: reason];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
}

Currently, when I subscribe in the second webview, then send from the original, the original does not have the same NSDictionary used and doesn't have the callbackId saved.
I don't know enough about cordova to properly address this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants