WebView
NoteWebView class is deprecated since API Level 10 and will be removed in API Level 12. The WebView provided by .NET MAUI or Tizen.NUI can be used instead.
You can access web pages and web content in your application using the WebView functionality. You can also use various other features for web browsing, such as loading and displaying web pages, and navigating through the browsing history.
The main features of WebView are as follows:
-
Initialize and create WebView
You can initialize a WebView and create a WebView object.
-
Manage web page loading
You can load a web page with requested URL and register event handlers for loading events.
-
Manage cookie
You can set the cookie policy and the storage for the cookie.
-
Execute JavaScript
You can execute the JavaScript code in the context of the current WebView.
-
Finalize WebView
You can shut down the WebView and clean up the resources.
Prerequisites
To enable your application to use WebView functionality, follow the steps below:
-
To use the Tizen.WebView namespace, create the
ElmSharp-Beta
project using the Project Wizard of Visual Studio. -
To use the class of the
Tizen.WebView
namespace, the application has to request permission by adding the following privileges to thetizen-manifest.xml
file:
XML
Copy
<privileges>
<!--To use the Internet connection-->
<privilege>http://tizen.org/privilege/internet</privilege>
<!--To access media storage-->
<privilege>http://tizen.org/privilege/mediastorage</privilege>
<!--To access, read, and write to the external storage-->
<privilege>http://tizen.org/privilege/externalstorage</privilege>
</privileges>
- To use the methods and properties of the
Tizen.WebView
namespace, include it in your application:C#Copyusing Tizen.WebView;
Initialize and create WebView
To Initialize WebView and create a WebView object, follow these steps:
-
To use the
Tizen.WebView
namespace in your application, call theTizen.WebView.Chromium.Initialize
method before the start of the main loop:C#CopyElementary.Initialize(); Elementary.ThemeOverlay(); Chromium.Initialize(); App app = new App(); app.Run(args);
-
To create a WebView object, use the constructor of Tizen.WebView.WebView with
ElmSharp.Window
:C#Copyvar webView = new WebView(window) { AlignmentX = -1, AlignmentY = -1, WeightX = 1, WeightY = 1 }; webView.Show();
Manage web page loading
To load a web page and add an event handler to loading events, follow these steps:
-
To load a web page, use the
Tizen.WebView.WebView.LoadUrl
method with an appropriate URL:C#CopywebView.LoadUrl("https://www.tizen.org/");
-
The following table lists the loading events provided by the
Tizen.WebView.WebView
class:
Table: Loading Events
Event | Description | Argument |
---|---|---|
LoadStarted |
This event occurs when the page loading has started. | - |
LoadFinished |
This event occurs when the page loading has completed. | - |
LoadError |
This event occurs when the page loading has failed with an error. | Tizen.WebView.SmartCallbackLoadErrorArgs |
-
Add an event handler to handle each loading event:
C#CopywebView.LoadStarted += (s, e) => { /* Handle LoadStarted event here */ }; webView.LoadFinished += (s, e) => { /* Handle LoadFinished event here */ }; webView.LoadError += (s, e) => { /* Handle LoadError event here */ // error code var code = e.Code; // description var desc = e.Description; // URL var url = e.Url; };
Manage cookie
To manage and set the cookie options, use the Tizen.WebView.CookieManager class in the following ways:
-
To get the
Tizen.WebView.CookieManager
object from WebView, useTizen.WebView.WebView.GetContext
andTizen.WebView.Context.GetCookieManager
:C#CopyCookieManager cookieManager = webView.GetContext().GetCookieManager();
-
To set the cookie acceptance policy, use the
Tizen.WebView.CookieManager.SetCookieAcceptPolicy
method:C#Copy/* Accepts every cookie sent from any page */ cookieManager.SetCookieAcceptPolicy(CookieAcceptPolicy.Always); /* Rejects all the cookies */ cookieManager.SetCookieAcceptPolicy(CookieAcceptPolicy.Never); /* Accepts only cookies set by the main document that is loaded */ cookieManager.SetCookieAcceptPolicy(CookieAcceptPolicy.NoThirdParty);
-
To set the persistent storage for the cookie, use the
Tizen.WebView.CookieManager.SetPersistentStorage
method:C#CopycookieManager.SetPersistentStorage(DirectoryInfo.Data, CookiePersistentStorage.SqlLite);
Execute JavaScript
To execute the JavaScript code, use the Tizen.WebView.Eval
and Tizen.WebView.EvalAsync
methods.
The following example shows the HTML file used:
HTML
Copy
<html>
<body>
<div id="main" class="page">
<div class="contents">
<span id="content-text">Basic</span>
</div>
</div>
</body>
</html>
-
If you need a return value, use the
Tizen.WebView.EvalAsync
method:C#Copystring result = await webView.EvalAsync("document.getElementById('content-text').innerHTML");
-
If you do not need a return value, use the
Tizen.WebView.Eval
method:C#CopywebView.Eval("document.getElementById('content-text').innerHTML = 'Tizen'");
Finalize WebView
To clean up the allocated resources, call the Tizen.WebView.Chromium.Shutdown
method after the end of the main loop:
C#
Copy
App app = new App();
app.Run(args);
Chromium.Shutdown();
Related information
- Dependencies
- Tizen 4.0 and Higher