How can I render html and/or non local website into a nativescript app? I cant find a way to start or manipulate a browser instance.
-
2docs.nativescript.org/ApiReference/ui/web-view/WebViewDamian Krawczyk– Damian Krawczyk2015-05-14 09:00:54 +00:00Commented May 14, 2015 at 9:00
2 Answers
The documentation will help you quite a bit, but here are two examples:
Example with url
This example will create a new page with a WebView on it and then navigate to that page. Url may be local (html file on phone) or remote (http://...)
var frameModule = require('ui/frame');
var pageModule = require('ui/page');
var webViewModule = require("ui/web-view");
var factoryFunc = function () {
var webView = new webViewModule.WebView();
webView.url = 'http://www.example.com';
var page = new pageModule.Page();
page.content = webView;
return page;
};
frameModule.topmost().navigate(factoryFunc);
Example of loading local data into a WebView
An example of where you have a view (.xml) and and its corresponding .js file and where you feed it a string containing the html to be displayed.
Where the .xml is:
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="loaded">
<WebView id="myWebView" />
</Page>
and the .js:
exports.loaded = function(args) {
var page = args.object;
var webView = page.getViewById('myWebView');
var application = require('application');
var html = '<html><body><h1>I can haz webview?</h1></body><html>';
if (application.ios) {
webView.ios.loadHTMLStringBaseURL(html, null);
} else if (application.android) {
webview.android.loadData(html, 'text/html', null);
}
};
4 Comments
In the upcoming release you will be able to specify the HTML like this:
<Page>
<WebView src="<html><body><h1>I can haz webview?</h1></body><html>" />
</Page>
Src property will work for URL and local file path as well.