Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-20018] iOS: Ti.App.fireEvent from webView is not working

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2015-11-23T20:11:17.000+0000
Affected Version/sRelease 5.0.2
Fix Version/sRelease 5.2.0
ComponentsiOS
Labelswebview
ReporterRamesh RAMAMURTHY
AssigneeSrikanth Sombhatla
Created2015-11-18T17:03:53.000+0000
Updated2016-01-19T21:45:40.000+0000

Description

The example from your site that I am trying to run is not functioning in IOS 9.1. I am interested in getting the event fired from the WebView to be caught in the application. This works in Android but not in IOS. I have tried it WillHandleTouches set to False.
<html>
	<head>
		<script>
			Ti.App.addEventListener("app:fromTitanium", function(e) {
	        	alert(e.message);
	      	});
		</script>
	</head>
	<body>
		<button onclick="Ti.App.fireEvent('app:fromWebView', { message: 'event fired from WebView, handled in Titanium' });">fromWebView</button>
	</body>
</html>

var win = Ti.UI.createWindow();
var webview = Ti.UI.createWebView({
	url: 'logging.html'
});
var button = Ti.UI.createButton({
	title: 'fromTitanium',
	height: '50dp',
	width: '130dp'
});
button.addEventListener('click', function(e) {
	Ti.App.fireEvent('app:fromTitanium', { message: 'event fired from Titanium, handled in WebView' });
});
Ti.App.addEventListener('app:fromWebView', function(e) {
	alert(e.message);
});
win.add(webview);
win.add(button);
win.open();

Comments

  1. Srikanth Sombhatla 2015-11-20

    This is seemingly a deadlock issue between the JS execution thread and main queue. For a quick work around, just delay the alert in JS so that main queue is not blocked. Change html to
       <html>
       <head>
       	<script>
       	Ti.App.addEventListener("app:fromTitanium", function(e) { 
       		setTimeout(function(){alert(e.message);}, 1);
       	});
       	</script>
       </head>
       <body>
       	<button onclick="Ti.App.fireEvent('app:fromWebView',
       	{ message: 'event fired from WebView, handled in Titanium' }
       	);">fromWebView</button>
       	</body>
       </html>
       
    In course we will fix this issue.
  2. Srikanth Sombhatla 2015-11-20

    PR https://github.com/appcelerator/titanium_mobile/pull/7477 Tested with reported test case html.
  3. Ramesh RAMAMURTHY 2015-11-20

    The solution given seems to be for message from the App to WebView, My issue is the other way around from the WebView to the app. can you please see if there is a work around for that.
  4. Ramesh RAMAMURTHY 2015-11-23

    May I get an update on this issue please? we will really appreciate an fix or workaround for this issue?
  5. Hans Knöchel 2015-11-23

    Hi [~rramesh1], the issues was just resolved as part of the 5.2.0 release. Using the fix, your test case works from both webview -> app and app -> webview.
  6. Ramesh RAMAMURTHY 2015-11-23

    when will this be released? May I get a preview version so that we can begin testing our app.
  7. Hans Knöchel 2015-11-23

    Hi [~rramesh1], the release is planned for next year, as we just released 5.1.0.GA last week. But: You can always grab the latest preview version. [Here](http://builds.appcelerator.com/mobile/master/mobilesdk-5.2.0.v20151123122025-osx.zip) is the latest 5.2.0 build including the fix, we hope that helps!
  8. Ramesh RAMAMURTHY 2015-11-29

    I will try it out, If I run into trouble with 5.2.0 may I get a patch on 5.1.1 please
  9. Hans Knöchel 2015-11-29

    Since it is no 5.1.0 related issue and 5.1.1 is already out, you will need to wait until 5.2.0 releases. But you can always patch your own version by following [this](http://docs.appcelerator.com/platform/latest/#!/guide/Building_the_Titanium_SDK_From_Source) instructions.
  10. Ramesh RAMAMURTHY 2015-11-29

    I used the 5.2.0 you gave and tested the message, from the webview-> app did not work. here is code that I used. var html = '' + '' + '' + '' + '' + "" + ''+ '' ; var win = Ti.UI.createWindow(); var webview = Ti.UI.createWebView({ html:html, top :50, willHandleTouches:false }); var button = Ti.UI.createButton({ title: 'fromTitanium', height: '50dp', width: '130dp' }); button.addEventListener('click', function(e) { Ti.App.fireEvent('app:fromTitanium', { message: 'event fired from Titanium, handled in WebView' }); }); Ti.App.addEventListener('app:fromWebView', function(e) { alert(e.message); }); win.add(webview); win.add(button); win.open();
  11. Ramesh RAMAMURTHY 2015-11-30

    Just want to clarify that this is an issue only in IOS
  12. Hans Knöchel 2015-11-30

    You are trying to embed a multiline html file inline, for which the property is not intended. Please try the fix in the scenario you stated in the original ticket description where you load the html using a local file.
  13. Ramesh RAMAMURTHY 2015-11-30

    I will change it to local file and test it later today
  14. Ramesh RAMAMURTHY 2015-12-01

    var html = '' + '' + '' + '' + '' + "" + ''+ '' ; var htmlFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "drive.html"); htmlFile.write(html); var win = Ti.UI.createWindow(); var webview = Ti.UI.createWebView({ //url:"https://saveonroad.com/test.html", url: htmlFile.getNativePath(), //html:html, top :50, willHandleTouches:false }); var button = Ti.UI.createButton({ title: 'fromTitanium', height: '50dp', width: '130dp' }); button.addEventListener('click', function(e) { Ti.App.fireEvent('app:fromTitanium', { message: 'event fired from Titanium, handled in WebView' }); }); Ti.App.addEventListener('app:fromWebView', function(e) { alert(e.message); }); win.add(webview); win.add(button); win.open();
  15. Ramesh RAMAMURTHY 2015-12-01

    tried these three scenarios , in none of the cases message from webview to App did not work. when the file is local or when using html string, app to webview message worked. When the file is remote messaging between the webview and the app did not work in both directions.
  16. Ramesh RAMAMURTHY 2015-12-01

    It works in one scenario, where the file exists on the local device, this file has to be static and we should not write to the file before opening it in the webview
  17. Hans Knöchel 2015-12-01

    Hi [~rramesh1], to clarify this once again: Sending and receiving events from the webview is *only* supposed to work with local html files. For example, your multi line inline had several syntax errors that prevented it from working. Please a) cache the html and display it or b) use a local html file from the beginning (recommended). If you still have issues, please check the [docs](http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.WebView) or the [Q&A](https://developer.appcelerator.com/questions/newest)
  18. Ramesh RAMAMURTHY 2015-12-02

    thanks Hans for your assistance it works now.
  19. Wilson Luu 2016-01-19

    Closing ticket as fixed. Verified that while using the sample code from the bug description and a local html file (as per Hans's comment), I was able to fire an event from the app to WebView and vice-versa. Tested on: Appc CLI NPM: 4.2.2 Appc CLI Core: 5.2.0-229 Arrow: 1.7.21 SDK: 5.2.0.v20160114021251 Node: v4.2.4 OS: El Capitan (10.11.2) Xcode: 7.2 Devices: iphone 6 (9.2)

JSON Source