-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
(iOS & Android) Add postMessage API support #362
Conversation
3327124
to
71b85e9
Compare
@janpio could you do a review of this PR when you get a minute? Travis CI tests failed for |
@@ -348,9 +358,6 @@ - (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command | |||
|
|||
- (void)injectDeferredObject:(NSString*)source withWrapper:(NSString*)jsWrapper | |||
{ | |||
// Ensure an iframe bridge is created to communicate with the CDVUIInAppBrowserViewController | |||
[self.inAppBrowserViewController.webView stringByEvaluatingJavaScriptFromString:@"(function(d){_cdvIframeBridge=d.getElementById('_cdvIframeBridge');if(!_cdvIframeBridge) {var e = _cdvIframeBridge = d.createElement('iframe');e.id='_cdvIframeBridge'; e.style.display='none';d.body.appendChild(e);}})(document)"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is createIframeBridge
equal to this?
Is it safe to move this to where createIframeBridge
is called now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
createIframeBridge
equal to this?
Yes, this code has been factorised out into createIframeBridge
Is it safe to move this to where
createIframeBridge
is called now?
In this original implementation, the iframe bridge is created "on demand", the first time that an injection method (such as injectScriptCode
) is called. Subsequent calls find the iframe bridge already exists, so it's only created once.
In the new implementation, createIframeBridge
is called after every successful page load (webViewDidFinishLoad
to ensure the bridge always exists, regardless of whether an injection method has been called. This enables pages contained within the IAB Webview to make use to the bridge in order to send message
events back to the app Webview, without relying on an injection method having been previously called in order to create the bridge.
Therefore, I think it's safe enough to do this.
Restarted the tests that now pass. What would be a use case for this new functionality, what problem does it solve? |
Currently we can inject code into the page inside the IAB Webview, using This PR enables asynchronous communication from page in the IAB Webview back to the parent Cordova app Webview via the A real-world use case in which I have used this functionality: I have a Cordova app which wraps a 3rd party website in the IAB. That website consists of various promotional pages which are CMS-managed and have volatile content (marketing team is constantly changing them) so cannot be placed statically inside the Cordova app. Some of those pages contain "Buy ticket" buttons which, if clicked in a desktop browser, open a popup window containing a pre-fill form to buy certain tickets. However when that page is loaded into the IAB, pressing that button does nothing (we can't open popup windows from the IAB). |
That does indeed sound like a very useful thing to do and pretty common use case. You might want to create a demo app and write a blog post about it... might come in handy when we release a new version of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useful functionality, code looks good.
Please some more review of people that actually know Java and Obj-C or tested this in their app.
Here's one I prepared earlier 😁 This demonstrates the new |
Nice! I would definitely use this feature, would allow me to remove a lot of hacky code i wrote to communicate data back and fourth from parent to child and back. |
Can you test this branch @keenan35i and see if it works for you? Would be a great help. |
For reference you can install my branch using:
Some out-of-the-box test code you can execute in any Cordova app Webview which contains this branch of the InAppBrowser plugin:
|
@infil00p @stevengill @agrieve @shazron @purplecabbage @jcesarmobile |
@keenan35i You have passed a simple string rather than stringified JSON to the The spec for the This is in the updated documentation:
So if I call But currently if I call In the case of Android and iOS UIWebView, we have custom implementations of |
oh i see, just tested it with an object and seems to work perfect! |
I have a use case that looks very similar to this. I'm trying to open an IAB to a login page hosted outside our Cordova app, and would like to close the IAB after the user successfully logs in (and has received their session cookies). @keenan35i If this PR will take some time to implement, would you be willing to share the hacky workaround code, so I can continue developing in the interim? 😄 |
This PR is complete (including documentation and examples) and ready for integration into the main plugin codebase - it is only awaiting review and approval. You can install and use the functionality using my branch mentioned above. |
Thanks @dpa99c @mattdsteele |
Unfortunately no - the endpoint we're using doesn't currently support OAuth 🤷♂️ |
The postMessage API as implemented by desktop browsers is intended for cross-frame communication, hence the API reflects this:
In the case of inappbrowser we are of course emulating this for cross-webview communication so the situation is different: The The
This implies that unserialized Javascript objects can be passed as the Note that in the case of WKWebView, it is the only implemntation where the
Docs updated to make this clearer. |
The Travis CI tests seem quite flaky: another couple of failures for no good reason 😞 |
I fixed CI. |
@purplecabbage I've tweaked as requested - anything else that concerns you, or do you think this is ready for merging? (@keenan35i is chomping at the bit 😀 ) |
Looks good to me.
… On Dec 12, 2018, at 1:50 AM, Dave Alden ***@***.***> wrote:
@purplecabbage I've tweaked as requested - anything else that concerns you, or do you think this is ready for merging? ***@***.*** is chomping at the bit 😀 )
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@janpio: if @purplecabbage is happy, can this be merged or does it need further review? |
Thanks for pinging me ;) |
@dpa99c Thanks, works great on my end. I was able to replace a localStorage polling implementation with this. Tested on both Android and iOS physical devices. |
Is it already included in 3.1.0?
Are any other plugins required to use |
Solution.
This way it works:
|
I got an type error, when I try to do post message using
I was using cordova-plugin-inappbrowser v3.2.0 kindly advice, if i am doing something wrong. |
After investigating, I have found out that this issue only present in cordova-plugin-inappbrowser v3.2.0... And this version not rise any issue with android, but cause issues with ios as I mentioned earlier. So, kindly fix this issue. |
@JoshuvaGeorge03 Please open a new issue with your problem description and link it here. Reporting bugs in merged or closed PRs and issue might get them lost. Did you use WKWebView before updating to version 3.2.0? |
@NiklasMerz Sorry, will create the new issue. Yes, My ionic app using WKWebView engine. is it mandatory to use WKWebView Engine for version 3.2.0? |
Yes it is |
@NiklasMerz kindly take a look at issue #613 |
@NiklasMerz Could you tell me is there any other requirement? and also i have created one repo for reproducing this issue.. kindly take a look at issue, which i have created. |
Platforms affected
Android
iOS (both UIWebView & WKWebView implementations)
What does this PR do?
Adds support for postMessage API 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