Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-27056] iOS: WebView HTML JS code can no longer access "Titanium." API (but "Ti." exists) as of 8.0.0

GitHub Issuen/a
TypeBug
PriorityNone
StatusClosed
ResolutionFixed
Resolution Date2019-06-04T22:27:08.000+0000
Affected Version/sRelease 8.0.0
Fix Version/sRelease 8.0.2
ComponentsiOS
LabelsWebView, engSchedule, html, ios, regression
ReporterJoshua Quick
AssigneeVijay Singh
Created2019-05-02T18:36:04.000+0000
Updated2019-06-04T22:27:08.000+0000

Description

*Summary:* The Titanium object is undefined within a WebView's HTML <script/> as of 8.0.0. Only the Ti object is defined. *Steps to reproduce:*

Set up a Classic Titanium "app.js" with the below code.

Build and run on iOS with Titanium 8.0.0 or 8.0.1.

Observe the log.

var htmlText =
		'<!DOCTYPE html>\n' +
		'<html>\n' +
		'	<head>\n' +
		'		<meta name="viewport" content="width=device-width, initial-scale=1.0">\n' +
		'	</head>\n' +
		'	<body>\n' +
		'		<p>HTML JavaScript Titanium API Test</p>\n' +
		'	</body>\n' +
		'	<script>\n' +
		'		document.addEventListener("DOMContentLoaded", function(e) {\n' +
		'			Ti.API.info("@@@ Logging via Ti.API.info() method.");\n' +
		'			Titanium.API.info("@@@ Logging via Titanium.API.info() method.");\n' +
		'			Ti.App.fireEvent("app:requestLogMessage", {\n' +
		'				message: "@@@ Fired event via Ti.App.fireEvent() method",\n' +
		'			});\n' +
		'			Titanium.App.fireEvent("app:requestLogMessage", {\n' +
		'				message: "@@@ Fired event via Titanium.App.fireEvent() method",\n' +
		'			});\n' +
		'			Ti.App.addEventListener("app:signalHtml", function eventHandler() {\n' +
		'				Ti.API.info("@@@ Received event via Ti.App.addEventListener() method.");\n' +
		'				Ti.App.removeEventListener("app:signalHtml", eventHandler);\n' +
		'			});\n' +
		'			Titanium.App.addEventListener("app:signalHtml", function eventHandler() {\n' +
		'				Titanium.API.info("@@@ Received event via Titanium.App.addEventListener() method.");\n' +
		'				Titanium.App.removeEventListener("app:signalHtml", eventHandler);\n' +
		'			});\n' +
		'			Ti.App.fireEvent("app:webViewReady", {})\n' +
		'		});\n' +
		'	</script>\n' +
		'</html>\n';

Ti.App.addEventListener("app:requestLogMessage", function(e) {
	Ti.API.info(e.message);
});
Ti.App.addEventListener("app:webViewReady", function(e) {
	Ti.App.fireEvent("app:signalHtml", {});
});

var window = Ti.UI.createWindow();
var webView = Ti.UI.createWebView({
	html: htmlText,
});
window.add(webView);
window.open();
*Expected Result:* The following should be logged. (You'll get the below with a 7.5.1 built app.)
[INFO]  @@@ Logging via Ti.API.info() method.
[INFO]  @@@ Logging via Titanium.API.info() method.
[INFO]  @@@ Fired event via Ti.App.fireEvent() method
[INFO]  @@@ Fired event via Titanium.App.fireEvent() method
[INFO]  @@@ Received event via Ti.App.addEventListener() method.
[INFO]  @@@ Received event via Titanium.App.addEventListener() method.
*Actual Result:* Only the 1st message gets logged in Titanium 8.0.0 or 8.0.1 because the script errors out when using the Titanium object. _(This is the bug.)_
[INFO]  @@@ Logging via Ti.API.info() method.
*Recommended Solution:* Add a var Titanium = Ti; to the code that gets injected into the WKWebView here... [TiUIWebView.m](https://github.com/appcelerator/titanium_mobile/blob/3d1569dededb35387b197a71317ffa3e7fb42ea8/iphone/Classes/TiUIWebView.m#L390) *Work-Around:* Use the Ti object instead of the Titanium object within the HTML's script.

Comments

  1. Vijay Singh 2019-05-03

    PR(master) - https://github.com/appcelerator/titanium_mobile/pull/10877 Note- Above PR is same as of ticket TIMOB-27055. Fixed both issue in same ticket in master. PR(8_0_X) - https://github.com/appcelerator/titanium_mobile/pull/10923
  2. Keerthi Mahalingam 2019-06-01

    FR Passed. Waiting on jenkins
  3. Keerthi Mahalingam 2019-06-03

    PR not merged for master. only merged on 8_0_X. waiting for resolving jenkin's failure
  4. Christopher Williams 2019-06-03

    merged to master, 8_0_X, 8_1_X
  5. Keerthi Mahalingam 2019-06-04

    Verified the fix on master SDK 8.2.0.v20190603133455, SDK 8.1.0.v20190603133458 and SDK 8.0.2.v20190603133031. logs shown in the console correctly.Closing {CODE} Operating System Name = Mac OS X Version = 10.13.6 Memory = 17179869184 Node.js Node.js Version = 8.9.1 npm Version = 5.5.1 Titanium CLI CLI Version = 5.1.1 Titanium SDK SDK Version = 8.1.0.v20190603133458 ,8.2.0.v20190603133455 and 8.0.2.v20190603133031 Device =Iphone 6s plus ios 12 Simulator . = iphone xR ios 12 {CODE}

JSON Source