Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-13145] iOS: Setting WebView HTML contents directly fails to load local JavaScript file on device

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2013-03-21T18:56:05.000+0000
Affected Version/sn/a
Fix Version/sRelease 3.1.0, 2013 Sprint 06 Core, 2013 Sprint 06
Componentsn/a
Labelsdevice, iOS, javascript, local, triage, webview
ReporterNathan Nadeau
AssigneeMax Stepanov
Created2013-02-28T01:15:12.000+0000
Updated2013-04-06T08:58:32.000+0000

Description

When I have a simple webview where I set the HTML via the Webview.html property (or alternatively by later calling Webview.setHtml() method), and the HTML string contains a reference to a local javascript file which is included under my Resources project directory, the application fails to load the local javascript file. But this only happens when installed on the target device. When run in the simulator it works as expected. If instead of creating the HTML dynamically and setting it via the html property or the setHtml method, I save the HTML to a local HTML file and load it via the Webview.url property, it then works as expected on both the simulator and the device, so the issue seems to be specifically related to the Webview.html property. Also, in Titanium SDK 3.0.0 this worked fine, but it stopped working in 3.0.2. To reproduce, create the following simple app.js file:
var html="<html>" + 
         "  <head>" +
         "    <script type='text/javascript' src='localjs/local.js'></script>" +
         "  </head>" +
         "  <body>" +
         "    <div onclick='doclick();'><p>Click Me!</p></div>" +
         "  </body>" +
         "</html>";
var webview = Ti.UI.createWebView({
   html: html,
   top: '5%',
   left: '5%',
   right: '5%',
   height: '90%',
   willHandleTouches: false
});
var win = Ti.UI.createWindow({layout: 'vertical'});
win.add(webview);
win.open();
And in localjs/local.js under Resources directory:
function doclick(){
   Ti.API.info('in local doclick function...');
   alert('you clicked on the webview!');
}
Then make sure you are using SDK 3.0.2 and run app.js. Expected: The app should load up the webview, and you should be able to click on the "Click Me!" text and you should see the corresponding alert() and Ti.API.info message. Actual: Using 3.0.2, on simulator, it works, but when you run it on the iOS device, nothing happens when you click on "Click Me!" text (no alert and no TI log message). And also while viewing the console log on the device via Xcode, you don't see the expected debug message saying that the local.js was loaded. If you revert to SDK 3.0.0 it works on the device as expected. Further notes: If instead of setting the Webview.html or using the Webview.setHtml() method, you save the HTML to a local file and then load that local file in the webview via Webview.url, then it seems to work in all cases. However for my purposes I need to dynamically generate HTML so it is not an ideal workaround to use static local files.

Comments

  1. Ingo Muschenetz 2013-03-04

    Please see if either of the related linked tickets seem similar to your issue.
  2. Ingo Muschenetz 2013-03-13

    Marking as duplicate. If this is an unrelated issue, please reopen.
  3. Nathan Nadeau 2013-03-14

    @Ingo, sorry I must have missed your comment from March 4. I have checked both of those other issues (TIMOB 9593 and TIMOB 13059), and in fact I had run across both those tickets while researching this issue. They all certainly seem to be related to WebView and the html property. However both of those tickets reference it as being an Android-only problem, and the workarounds/fixes might only apply to Android. This ticket deals with an issue with iOS only, and again only when the app is installed on the device, so I entered it as a new ticket. Additionally since it worked in 3.0.0 but not 3.0.2, I wanted to enter it as a new regression item. Those other tickets may have existed before this regression was introduced. But it's certainly possible the above problem is related to the problems listed in those other tickets.
  4. Ingo Muschenetz 2013-03-14

    @Nathan--can you try it in 3.1.0 (master) from the CI server and see what behavior you see there?
  5. Nathan Nadeau 2013-03-14

    @Ingo unfortunately 3.1.0 (master) from CI server did NOT solve the problem. It still fails to load the local javascript file on the iOS device only (works on the simulator). I think this is a different issue than the Android issues, which seem to have been resolved according to those other items.
  6. Max Stepanov 2013-03-21

    I do not believe it's a regression. SDK build scripts analyze included html files for any .js references and keep these .js files as-is w/o packaging into binary. Thus making them available for WebView to load. It never worked for html property because build scripts do not process them for that purpose. However, with the fix made at https://github.com/appcelerator/titanium_mobile/pull/3701 it is now possible to reference packaged .js files from WebView.
  7. Max Stepanov 2013-03-21

    PR https://github.com/appcelerator/titanium_mobile/pull/3999
  8. Paras Mishra 2013-04-06

    local JavaScript file ie localjs/local.js inside Resources folder is loaded on the device, when the app is executed. Alert is displayed and "in local doclick function..." is also logged on console. Verified on: Device : iPad mini, iOS version: 6+ SDK: 3.1.0.v20130405170202 CLI version : 3.1.0-beta OS : MAC OSX 10.7.5 Appcelerator Studio, build: 3.1.0.201304011603 XCode : 4.5.1

JSON Source