Web view not working in Mobile Web app (Blank.html)

You must Login before you can answer or comment on any questions.

Application type: Mobile

Titanium SDK: 2.1.1.GA

Platform: Mobile Web

Hello,

I want to use Google Analytics in my Mobile Web app. Therefore i used a Webview. When i run this on Mac OS X in Safari it works great. But when i package the app and put it on a server, the webview is not working on safari and on my android phone (android 2.3.3). On safari it downloads an blank.html instead of showing the webview.

Here's is the code i use:

var webView = Titanium.UI.createWebView({
    html : analyticHtml //String with Google Analytic stuff
});
Is this a bug in Mobile Web or I'm doing something wrong?

1 Answer

Accepted Answer

WebViews on Mobile Web use iframe elements. The way it's suppose to work is when you create a WebView without specifying a URL, it will point the iframe to a blank.html. Since the blank.html is served from the same domain as your app, we can insert the HTML into the iframe.

One caveat is that by the time the HTML is inserted into the iframe, the iframe's window.onload event has already fired.

It's possible that your snippet may be waiting for onload to fire, but onload has already fired.

It's also possible that your analyticsHtml string doesn't contain <html> and <body> tags, which might be throwing off the analytics JavaScript code.

You also may find it easier to just evaluate the Google Analytics code directly from the app.js:

if (Ti.Platform.name == 'mobileweb') {
    (function () {
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-XXXXX-X']);
        _gaq.push(['_trackPageview']);
        var ga = document.createElement('script');
        ga.type = 'text/javascript';
        ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(ga, s);
    }())
}

Your Answer

Think you can help? Login to answer this question!