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

feat(android): webview injectEventBindings method for external pages #13835

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

m1ga
Copy link
Contributor

@m1ga m1ga commented May 12, 2023

  • in the last "to arrow function" change on parameter was missed causing it the return an error.

  • Adding injectEventBindings to the Android webview so you don't have to run a custom evalJS script yourself in the app to inject the event bindings into external pages.

  • minified bindings.js.min again

I still keep the example in the docs if you want to add it manually in case you'll need to make changes (but with less code!). For iOS it is not needed since you can use window.webkit.messageHandlers.yourHandlerName.postMessage... to communicate with the app right away. The manual evalJS code could be used for iOS too!

var win = Ti.UI.createWindow();

var btn = Ti.UI.createButton({
	title: "add"
});
var webview = Ti.UI.createWebView({
	url: "https://example.com"
});

win.add([webview, btn]);
win.open();

btn.addEventListener('click', function(e) {
	webview.injectEventBindings();
});

Before
Screenshot_20230512_104730

After clicking the button:
Screenshot_20230512_111113

Then you can add other scripts that will fire Ti.App.fireEvents() to your page.

apidoc/Titanium/UI/WebView.yml Outdated Show resolved Hide resolved

For iOS check the example in <Titanium.UI.WebView.addScriptMessageHandler>.
<b>iOS</b>:
For iOS have a look at the example in <Titanium.UI.WebView.addScriptMessageHandler> for custom handler names or if you run the manual evalJS version
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parity is important. If iOS uses addScriptMessageHandler(), then either Android should match or we should deprecate addScriptMessageHandler() and add injectEventBindings() to iOS and have addScriptMessageHandler() call it so we don't break backwards compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parity would be good. I just didn't know how to integrate the injectEventBindings() in ObjectivC 😊 That's why I wanted to keep the evalJS example since that is working on both platforms. window.webkit.messageHandlers.yourHandlerName is not available in the Android webview so we can't directly map that.

The default Ti way to communicate from webview -> app is still Ti.App.fireEvent and that is cross-platform. The docs were just always wrong saying that you can't use it with external URL, you could always inject that code with evalJS (or if it is your external page: just put the file on your server and load it right away) 😄

I'll try to see if I can add the iOS part!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this PR is looking good! We can dive into the iOS stuff during our meeting. Great job!

apidoc/Titanium/UI/WebView.yml Outdated Show resolved Hide resolved
@m1ga
Copy link
Contributor Author

m1ga commented Sep 12, 2024

🤔 I broke it after merging master....strange. Master is still building fine, this PR is failing. Checking at the moment.

edit: fixed. Code was in there twice

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

Successfully merging this pull request may close these issues.

2 participants