(iOS & Android) Add postMessage API support (#362)
<!-- Please make sure the checklist boxes are all checked before submitting the PR. The checklist is intended as a quick reference, for complete details please see our Contributor Guidelines: http://cordova.apache.org/contribute/contribute_guidelines.html Thanks! --> ### Platforms affected Android iOS (both UIWebView & WKWebView implementations) ### What does this PR do? Adds support for [postMessage API](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) enabling pages loaded into the InappBrowser to post messages back to the parent Webview of the Cordova app. For example, sending event messages associated with UI interactions such as button clicks from the wrapped page back to the parent app Webview. ### What testing has been done on this change? Automated tests have been extended to cover the `message` event. ### Checklist - [x ] Commit message follows the format: "GH-3232: (android) Fix bug with resolving file paths", where CB-xxxx is the JIRA ID & "android" is the platform affected. - [ x] Added automated test coverage as appropriate for this change.
This commit is contained in:
committed by
Jan Piotrowski
parent
0fd43ae644
commit
c54d10052a
+28
-14
@@ -555,23 +555,37 @@ static CDVWKInAppBrowser* instance = nil;
|
||||
|
||||
CDVPluginResult* pluginResult = nil;
|
||||
|
||||
NSDictionary* messageContent = (NSDictionary*) message.body;
|
||||
NSString* scriptCallbackId = messageContent[@"id"];
|
||||
|
||||
if([messageContent objectForKey:@"d"]){
|
||||
NSString* scriptResult = messageContent[@"d"];
|
||||
NSError* __autoreleasing error = nil;
|
||||
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[scriptResult dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
|
||||
if ((error == nil) && [decodedResult isKindOfClass:[NSArray class]]) {
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:(NSArray*)decodedResult];
|
||||
if([message.body isKindOfClass:[NSDictionary class]]){
|
||||
NSDictionary* messageContent = (NSDictionary*) message.body;
|
||||
NSString* scriptCallbackId = messageContent[@"id"];
|
||||
|
||||
if([messageContent objectForKey:@"d"]){
|
||||
NSString* scriptResult = messageContent[@"d"];
|
||||
NSError* __autoreleasing error = nil;
|
||||
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[scriptResult dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
|
||||
if ((error == nil) && [decodedResult isKindOfClass:[NSArray class]]) {
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:(NSArray*)decodedResult];
|
||||
} else {
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION];
|
||||
}
|
||||
} else {
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION];
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];
|
||||
}
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
|
||||
}else if(self.callbackId != nil){
|
||||
// Send a message event
|
||||
NSString* messageContent = (NSString*) message.body;
|
||||
NSError* __autoreleasing error = nil;
|
||||
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[messageContent dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
|
||||
if (error == nil) {
|
||||
NSMutableDictionary* dResult = [NSMutableDictionary new];
|
||||
[dResult setValue:@"message" forKey:@"type"];
|
||||
[dResult setObject:decodedResult forKey:@"data"];
|
||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dResult];
|
||||
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
|
||||
}
|
||||
} else {
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];
|
||||
}
|
||||
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
|
||||
}
|
||||
|
||||
- (void)didStartProvisionalNavigation:(WKWebView*)theWebView
|
||||
|
||||
Reference in New Issue
Block a user