Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-974] Android: Webview.html returns null if webview is based on URL

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2014-02-06T18:19:02.000+0000
Affected Version/sRelease 1.7.0
Fix Version/sSprint 2012-01, Release 2.0.0, Release 1.8.1, 2014 Sprint 02, 2014 Sprint 02 API, Release 3.2.1, Release 3.3.0
ComponentsAndroid
Labelsandroid, defect, module_webview, parity, qe-3.2.1, qe-testadded, regression
ReporterBill Dawson
AssigneeHieu Pham
Created2011-04-15T02:40:35.000+0000
Updated2014-02-06T18:19:50.000+0000

Description

Problem

The html property of a web view returns undefined for remote URLs.

Sample Code

The following will test if the html property is convincingly set for a reliable remote URL (the server always returns the text "GET"). If it is, a green border will show up. If it fails, a red border will show up.
/**
 * This tests sees if the Ti.UI.WebView will properly return the HTML of a remote website.
 * (And it gets ornery if it fails to grab the HTML.)
 */
var win = Ti.UI.createWindow({ backgroundColor: '#fff' });
var web = Ti.UI.createWebView({
    url: 'http://appc.me/test/Echo',
    width: '90%', height: '90%', top: '5%', left: '5%'
});
win.add(web);
win.open();
web.addEventListener('load', function() {
    // The server returns just 'GET', but iOS forces the web view to be well formed...
    Ti.API.info(web.html);
    // So we'll accept either a well formed HTML or the literal response from the server.
    if (web.html == '<html><head></head><body>GET</body></html>' || web.html == 'GET') {
        win.backgroundColor = '#0f0';
        alert('PASS: I love you forever!');
    }
    // Otherwise... FAIL!
    else {
        win.backgroundColor = '#f00';
        alert('FAIL: web.html != expected html');
    }
});

Associated Helpdesk Ticket

None, but there is a Staff Augmentation project that needs this for their Android app to work.

Comments

  1. Dawson Toth 2011-06-01

    Workaround

    You can get around this issue in Android by grabbing the HTML yourself with some JavaScript. So, the above sample code becomes...
       /**
        * This tests sees if the Ti.UI.WebView will properly return the HTML of a remote website.
        * (And it gets ornery if it fails to grab the HTML.)
        */
       var win = Ti.UI.createWindow({ backgroundColor: '#fff' });
       var web = Ti.UI.createWebView({
           url: 'http://appc.me/test/Echo',
           width: '90%', height: '90%', top: '5%', left: '5%'
       });
       win.add(web);
       win.open();
       web.addEventListener('load', function() {
           // The server returns just 'GET', but iOS forces the web view to be well formed...
           var html = web.html || web.evalJS("'<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'");
           Ti.API.info(html);
           // So we'll accept either a well formed HTML or the literal response from the server.
           if (html == '<html><head></head><body>GET</body></html>' || web.html == 'GET') {
               win.backgroundColor = '#0f0';
               alert('PASS: I love you forever!');
           }
           // Otherwise... FAIL!
           else {
               win.backgroundColor = '#f00';
               alert('FAIL: web.html != expected html');
           }
       });
       
  2. Wilson Luu 2012-01-11

    Closing bug. Verified fix on: SDK build: 1.9.0.v20120111103135 Runtime: V8, Rhino Titanium Studio, build: 1.0.8.201201101928 Device: Droid 3 (2.3.4)
  3. Wilson Luu 2012-01-12

    Updating tags
  4. Wilson Luu 2012-01-23

    Updating tags
  5. jithinpv 2013-11-25

    Anvil test case added. PR link: https://github.com/appcelerator/titanium_mobile/pull/5020
  6. Paras Mishra 2013-11-28

    I am able to reproduce this issue. This is regression, works fine on SDK: 3.1.3.GA Using Environment: Device : Google Nexus 7, Android Version: 4.2.1 SDK: 3.2.0.v20131127194046 CLI version : 3.2.0-beta OS : MAC OSX 10.9 Alloy: 1.3.0 ACS: 1.0.9 Appcelerator Studio, build: 3.2.0.201311272052 titanium-code-processor: 1.1.0-beta XCode : 5.0.2
  7. Hieu Pham 2014-01-29

    I've tested and confirmed that this is not a regression. It looks like a regression but CLI 3.2.0 is actually exposing this issue. This is because the latest CLI always target the latest SDK by default, whereas the 3.1.3 CLI targets API 10 by default. This issue is happening on API 17+ (4.2+). Starting from API 17, Android introduced @JavascriptInterface annotation that needs to be added to every function that exposes to Javascript. https://github.com/appcelerator/titanium_mobile/pull/3932 fixed most of these, but setValue() was left out.
  8. Hieu Pham 2014-01-30

    master PR: https://github.com/appcelerator/titanium_mobile/pull/5287
  9. Vishal Duggal 2014-01-30

    PR merged
  10. Vishal Duggal 2014-01-30

    Backport to 3_2_X https://github.com/appcelerator/titanium_mobile/pull/5294
  11. Samuel Dowse 2014-02-05

    Verified fixed on: Mac OSX 10.9.2 Appcelerator Studio, build: 3.2.1.201402041206 Titanium SDK, build: 3.2.1.v20140203174850 CLI: 3.2.1-beta3 Alloy: 1.3.1-cr2 Android Emulator: 2.3.3, 4.2.2 and 4.4.2 Android Device: 4.4.2 Both code in description and comment passes successfully. Closing

JSON Source