UniWebView

Summary

The main class of UniWebView. It represents a native web view and exposes a few APIs for you to use in Unity. You could create and use an instance of UniWebView to show a page from URL, interact with the web content, as well as receive a message from the web view.

Properties Summary

Whether the web view is supported in current runtime or not.

Gets or sets the frame of current web view.

A reference rect transform which the web view should change its position and size to.

The url of current loaded web page.

Gets whether there is a back page in the back-forward list that can be navigated to.

Gets whether there is a forward page in the back-forward list that can be navigated to.

Gets or sets the background color of web view.

Gets or sets the alpha value of the whole web view.

Represents the embedded toolbar in the current web view.

Events Summary

Raised when the web view starts loading a url.

Raised when the web view finished to load a url successfully.

Raised when the loading progress value changes in current web view.

Raised when an error encountered during the loading process.

Raised when an error encountered during the loading process.

Raised when an image captured and stored in a cache path on disk.

Raised when a file download task starts.

Raised when a file download task finishes with either an error or success.

Raised when a message from web view is received.

Raised when the web view is about to close itself.

Raised when the screen orientation is changed.

On iOS, raise when the system calls webViewWebContentProcessDidTerminate method.

Raised when a new window is opened.

Raised when the multiple window is closed.

Raised when a key (like back button or volume up) on the device is pressed.

Methods Summary

Loads a url in current web view.

Loads an HTML string in current web view.

Reloads the current page.

Stops loading all resources on the current page.

Navigates to the back item in the back-forward list.

Navigates to the forward item in the back-forward list.

Sets whether the link clicking in the web view should open the page in an external browser.

Sets the web view visible on screen.

Sets the web view invisible from screen.

Animates the web view from current Frame (position and size) to another Frame (position and size) within duration.

Updates and sets current frame of web view to match the setting.

Adds a JavaScript to current page.

Evaluates a JavaScript string on current page.

Adds a url scheme to UniWebView message system interpreter.

Removes a url scheme from UniWebView message system interpreter.

Adds a domain to the SSL checking white list.

Removes a domain from the SSL checking white list.

Sets a customized header field for web view requests.

Sets the user agent used in the web view.

Gets the user agent string currently used in web view.

Sets the adjustment behavior which indicates how safe area insets are added to the adjusted content inset.

Sets allow auto-play for current web view.

Sets allow inline play for current web view.

Sets whether loading a local file is allowed.

Sets whether file access from file URLs is allowed.

Sets whether the UniWebView should allow third party cookies to be set.

Sets allow universal access from file URLs.

Sets whether the web view area should avoid soft keyboard.

Sets whether JavaScript should be enabled in current web view.

Sets whether the web view limits navigation to pages within the app’s domain.

Sets whether JavaScript can open windows without user interaction.

Sets whether a callout (context) menu should be displayed when user long tapping on certain web view content.

Sets whether the web view should support a pop up web view triggered by user in a new tab.

Sets the default font size used in the web view.

Sets the text zoom used in the web view.

Sets whether the drag interaction should be enabled on iOS.

Sets the way of how the cache is used when loading a request.

Cleans web view cache.

Sets whether the web page console output should be forwarded to native console.

Sets a cookie for a certain url.

Gets the cookie value under a url and key.

Removes all the cookies under a url.

Removes the certain cookie under a url for the specified key.

Clear all cookies from web views.

Clears any saved credentials for HTTP authentication for both Basic and Digest.

Sets whether to show a loading indicator while the loading is in progress.

Sets the text displayed in the loading indicator, if SetShowSpinnerWhileLoading is set to true.

Sets whether the user can dismiss the loading indicator by tapping on it or the greyed-out background around.

Shows the loading spinner.

Hides the loading spinner.

Sets whether the horizontal scroll bar should show when the web content beyonds web view bounds.

Sets whether the vertical scroll bar should show when the web content beyonds web view bounds.

Sets whether the web view should show with a bounces effect when scrolling to page edge.

Sets whether the web view supports zoom gesture to change content size.

Adds a trusted domain to white list and allow permission requests from the domain.

Removes a trusted domain from white list.

Sets whether the device back button should be enabled to execute "go back" or "closing" operation.

Sets whether the web view should enable support for the "viewport" HTML meta tag or should use a wide viewport.

Sets whether the web view loads pages in overview mode, that is, zooms out the content to fit on screen by width.

Sets whether the web view should behave in immersive mode, that is, hides the status bar and navigation bar with a sticky style.

Sets whether to show a toolbar which contains navigation buttons and Done button.

Sets the done button text in toolbar.

Sets the go back button text in toolbar.

Sets the go forward button text in toolbar.

Sets the visibility of navigation buttons, such as "Go Back" and "Go Forward", on toolbar.

Sets the background tint color for the toolbar.

Sets the button text color for the toolbar.

Sets whether the web view can receive user interaction or not.

Sets whether the web view should pass through clicks at its clear pixels to Unity scene.

Enables debugging of web contents.

Enables user resizing for web view window.

Sets whether horizontal swipe gestures should trigger back-forward list navigation.

Sets whether a prompt alert should be displayed for collection username and password when the web view receives an HTTP authentication challenge (HTTP Basic or HTTP Digest) from server.

Gets the HTML content from current page by accessing its outerHTML with JavaScript.

Prints current page.

Capture the content of web view and store it to the cache path on disk with the given file name.

Starts the process of continually rendering the snapshot.

Stops the process of continually rendering the snapshot.

Gets the data of the rendered image for the current web view.

Creates a rendered texture for the current web view.

Scrolls the web view to a certain point.

Adds the URL to download inspecting list.

Removes the URL from download inspecting list.

Adds the MIME type to download inspecting list.

Removes the MIME type from download inspecting list.

Sets whether allowing users to edit the file name before downloading.

Sets whether allowing users to choose the way to handle the downloaded file.

Sets whether the OnFileDownloadStarted and OnFileDownloadFinished events should be raised even for an image saving action triggered by the callout (context) menu on Android.

Registers a method handler for deciding whether UniWebView should handle the request received by the web view.

Unregisters the method handler for handling request received by the web view.

Registers a method handler for deciding whether UniWebView should allow a media request from the web page or not.

Unregisters the method handler for handling media capture permission request.

Properties

Whether the web view is supported in current runtime or not.

On some certain Android customized builds, the manufacturer prefers not containing the web view package in the system or blocks the web view package from being installed. If this happens, using of any web view related APIs will throw a MissingWebViewPackageException exception.

Use this method to check whether the web view is available on the current running system. If this parameter returns false, you should not use the web view.

This property always returns true on other supported platforms, such as iOS or macOS editor. It only performs runtime checking on Android. On other not supported platforms such as Windows or Linux, it always returns false.

Example

if (UniWebView.IsWebViewSupported) {
    // Do other things with UniWebView.
}

Gets or sets the frame of current web view. The value is based on current Screen.width and Screen.height. The first two values of Rect is x and y position and the followed two width and height. The original point is top left corner:

NOTICE

Frame will be ignored if ReferenceRectTransform is set.

Example

// Make the web view full screen:
webView.Frame = new Rect(0, 0, Screen.width, Screen.height);

// Make the web view center in the screen with size 500x500:
var side = 500;
var x = (Screen.width - side) / 2.0f;
var y = (Screen.height - side) / 2.0f;
webView.Frame = new Rect(x, y, side, side);

A reference rect transform which the web view should change its position and size to.

Set it to a Unity UI element (which contains a RectTransform) under a canvas to determine the web view frame by a certain UI element.

By using this, you could get benefit from Multiple Resolutions UI.

Example

// Some panel
RectTransform panel = ...

// Set the web view position and size to match panel
webView.ReferenceRectTransform = panel;

The url of current loaded web page.

Example

webView.Load("https://example.com/");

// Some time later or in "OnPageFinished":
print(webView.Url);
// => "https://example.com/"

Gets whether there is a back page in the back-forward list that can be navigated to.

Gets whether there is a forward page in the back-forward list that can be navigated to.

Gets or sets the background color of web view. The default value if Color.white.

This only sets the background color of the content view of the web view. Normally, the background color will be hidden by the web page background. If you want to make the web view background visible, you need to make the web page it transparent by adding some necessary style to it.

This property only changes the web view background. If you want to make the whole web view transparent, use Alpha instead.

Example

// Set the web view background (under the web page) to red.
webView.BackgroundColor = Color.red;

Gets or sets the alpha value of the whole web view.

You can make the game scene behind web view visible to make the web view transparent.

The default value is 1.0f, which means totally opaque. Set it to 0.0f will make the web view totally transparent.

Example

// Set the web view half transparent.
webView.Alpha = 0.5f;

Represents the embedded toolbar in the current web view.

A toolbar is a built-in area inside the web view, shown at the top or bottom of the web view. It contains navigation buttons like "Back" and "Forward", as well as a "Done" button to close the web view. You can use APIs under UniWebViewEmbeddedToolbar to customize the toolbar. See its APIs for more details.

Example

// Show the embedded toolbar.
webView.EmbeddedToolbar.Show();

Events

Raised when the web view starts loading a url.

This event will be invoked for both URL loading with Load method or by a link navigating from a page.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • string url

    The url which the web view is about to load.

Example

webView.OnPageStarted += (view, url) => {
    print("Loading started for url: " + url);
};
webView.Load("https://example.com");

// => "Loading started for url: https://example.com/"

Raised when the web view finished to load a url successfully.

This method will be invoked when a valid response received from the URL, regardless of the response status. If a URL loading fails before reaching to the server and getting a response, OnLoadingErrorReceived will be raised instead.

NOTICE

Android did not provide a way to get the HTTP status code until API Level 23 (Android 6). The statusCode is not trustable and will be always 200 on Android devices running a system before Android 6.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • int statusCode

    HTTP status code received from response.

  • string url

    The url which the web view begins to load.

Example

webView.OnPageFinished += (view, statusCode, url) => {
    print(statusCode);
    print("Web view loading finished for: " + url);
};

webView.Load("https://example.com");
// => "202"
// => "Web view loading finished for: https://example.com"

webView.Load("https://some_domain.com/404");
// => "404"
// => "Web view loading finished for: https://example.com"

Raised when the loading progress value changes in current web view.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • float progress

    A value indicates the loading progress of current page. It is a value between 0.0f and 1.0f.

Example

webView.OnPageProgressChanged += (view, progress) => {
    Debug.Log("Progress: " + progress);
};

webView.Load("https://uniwebview.com/");

// => "Progress: 0.1"
// => "Progress: 0.3"
// => "Progress: 0.52"
// => "Progress: 0.87"
// => "Progress: 1.0"

Raised when an error encountered during the loading process. Such as the "host not found" error or "no Internet connection" error will raise this event.

NOTICE

This event is deprecated. Use OnLoadingErrorReceived instead. If both OnPageErrorReceived and OnLoadingErrorReceived are listened, only the new OnLoadingErrorReceived will be raised, OnPageErrorReceived will not be called.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • int errorCode

    The error code which indicates the error type. It can be different from systems and platforms.

  • string errorMessage

    The error message which describes the detail of error.

Example

webView.OnPageErrorReceived += (view, error, message) => {
    print("Error.");
};

webView.Load("https://site-not-existing.com/");
// => "Error."

webView.Load("unknown://host?param1=value1&param2=value2");
// => "Error."

webView.Load("https://self-signed.badssl.com");
// => "Error."

Raised when an error encountered during the loading process. Such as the "host not found" error or "no Internet connection" error will raise this event.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • int errorCode

    The error code which indicates the error type. It can be different from systems and platforms.

  • string errorMessage

    The error message which describes the detail of error.

  • UniWebViewNativeResultPayload payload

    The payload received from native side, which contains the error information, such as the failing URL, in its Extra.

Example

webView.OnLoadingErrorReceived += (view, error, message, payload) => {
    print("Error.");
    if (payload.Extra != null && 
        payload.Extra.TryGetValue(UniWebViewNativeResultPayload.ExtraFailingURLKey, out var value)) 
    {
        var url = value as string
        // The `url` contains the failing URL causes the error, if available.
    }
};

webView.Load("https://site-not-existing.com/");
// => "Error."

webView.Load("unknown://host?param1=value1&param2=value2");
// => "Error."

webView.Load("https://self-signed.badssl.com");
// => "Error."

Raised when an image captured and stored in a cache path on disk.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • int errorCode

    The error code of the event. If the snapshot is captured and stored without a problem, the error code is 0. Any other number indicates an error happened. In most cases, the screenshot capturing only fails due to lack of disk storage.

  • string diskPath

    An accessible disk path to the captured snapshot image. If an error happens, it is an empty string.

Example

webView.OnCaptureSnapshotFinished += (view, errorCode, filePath) => {
    if (errorCode != 0) { return; }
    byte[] bytes = File.ReadAllBytes(filePath);
    Texture2D texture = new Texture2D(2, 2, TextureFormat.RGB24, false);
    texture.LoadImage(bytes);

    // Use the texture.
};
webView.CaptureSnapshot("sample.png");

Raised when a file download task starts.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • string remoteUrl

    The remote URL of this download task. This is also the download URL for the task.

  • string fileName

    The file name which user chooses to use.

Example

webView.OnFileDownloadStarted += (view, remoteUrl, fileName) => {
    print(string.Format("Download Started. From '{0}', file name '{1}'", remoteUrl, fileName));
};

Raised when a file download task finishes with either an error or success.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • int errorCode

    The error code of the download task result. Value 0 means the download finishes without a problem. Any other non-0 value indicates an issue. The detail meaning of the error code depends on system. On iOS, it is usually the errorCode of the received NSURLError. On Android, the value usually represents an ERROR_* value in DownloadManager.

  • string remoteUrl

    The remote URL of this download task.

  • string diskPath

    The file path of the downloaded file. On iOS, the downloader file is in a temporary folder of your app sandbox. On Android, it is in the "Download" folder of your app.

Example

webView.OnFileDownloadFinished += (view, errorCode, remoteUrl, diskPath) => {
    if (errorCode == 0) { // Success
        print(string.Format("Download Finished. From '{0}', to '{1}'", remoteUrl, diskPath));
    } else {
        print("Download error: " + errorCode);
    }
};

Raised when a message from web view is received.

Generally, the message comes from a navigation to a scheme which is observed by current web view. You could use AddUrlScheme and RemoveUrlScheme to manipulate the scheme list.

"uniwebview://" scheme is default in the list, so a clicking on link starting with "uniwebview://" will raise this event, if it is not removed.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • UniWebViewMessage message

    The message object which contains information like message path and arguments.

Example

webView.OnMessageReceived += (view, message) => {
    print(message.Scheme);
    print(message.Path);
    print(message.Args["param1"]);
    print(message.Args["param2"]);
}
// Run the JavaScript below in the web page:
// location.href = "uniwebview://host?param1=value1&param2=value2"

// => "uniwebview", "host", "value1", "value2"

anotherWebView.OnMessageReceived += (view, message) => {
    print(message.RawMessage);
}
anotherWebView.AddUrlScheme("myscheme");

// Click the link "myscheme://action" in a web page.
// <a href="myscheme://action">Click Me</a>

// => "myscheme://action"

Raised when the web view is about to close itself.

This event is raised when the users close the web view by the Back button on Android, the Done button on iOS, or the Close button on Unity Editor. It gives a chance to make a final decision whether the web view should be closed and destroyed. You can also clean all related resources you created (such as a reference to the web view) in this event.

If this event is not listened and implemented, the web view will be closed and destroyed by default when it needed.

Parameters
  • UniWebView webView

    The web view component which raises this event.

Return Value

Whether the web view should be closed and destroyed.

Example

// Clean webView field when 
public class MyBehaviour : MonoBehaviour {
    var webView;

    void Start() {
        webView = gameObject.AddComponent<UniWebView>();
        webView.OnShouldClose += (view) => {
            webView = null;
            return true;
        };
    }
}

// Make the web view there without being closed
webView.OnShouldClose += (view) => {
    return false;
};

Raised when the screen orientation is changed. It is a good time to set the web view frame if you need to support multiple orientations in your game.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • ScreenOrientation orientation

    The screen orientation for current state.

Example

// Keep the web view full screen on both portrait and landscape mode.
webView.Frame = new Rect(0, 0, Screen.width, Screen.height);
webView.OnOrientationChanged += (view, orientation) => {
    webView.Frame = new Rect(0, 0, Screen.width, Screen.height);
};

On iOS, raise when the system calls webViewWebContentProcessDidTerminate method. On Android, raise when the system calls onRenderProcessGone method.

It is usually due to a low memory or the render process crashes when loading the web content. When this happens, the web view will leave you a blank white screen.

Usually you should close the web view and clean all the resource since there is no good way to restore. In some cases, you can also try to free as much as memory and do a page Reload.

Parameters
  • UniWebView webView

    The web view component which raises this event.

Example

// Clean memory and reload current page
webView.OnWebContentProcessTerminated += (view) => {
    // Free memory
    // unusedAssets.Clean();

    webView.Reload();
};

Raised when a new window is opened. This happens when you enable the SetSupportMultipleWindows and open a new pop-up window.

Parameters
  • UniWebView webView

    The web view component which opens the new multiple (pop-up) window.

  • string multipleWindowId

    The identifier of the opened new window.

Example

string newWindow = "";
webView.OnMultipleWindowOpened += (view, windowId) => {
    // A new window with identifier "windowId" is opened.
};

Raised when the multiple window is closed. This happens when the pop-up window is closed by navigation operation or by a invocation of close() on the page.

Parameters
  • UniWebView webView

    The web view component which closes the multiple window.

  • string multipleWindowId

    The identifier of the closed new window.

Example

webView.OnMultipleWindowClosed += (view, windowId) => {
    // The opened window with identifier "windowId" is closed.
};

Raised when a key (like back button or volume up) on the device is pressed.

This event only raised on Android. It is useful when you disabled the back button but still need to get the back button event. On iOS, user's key action is not available and this event will never be raised.

NOTICE

This event is deprecated from version 4.0. Now UniWebView never intercepts device key code events. So this event will be never raise anymore. Check Input.GetKeyUp in Update() instead.

Parameters
  • UniWebView webView

    The web view component which raises this event.

  • int keyCode

    The key code of pressed key. See Android API for keycode to know the possible values.

Example

// DON'T DO IT.
// webView.OnKeyCodeReceived += (view, keyCode) => {
//     if (keyCode == 4) {
//         Debug.Log("Back Button was clicked.");
//     }
// };

// Check Input in Update():
void Update() {
    if (Input.GetKeyUp(KeyCode.Escape)) {
        Debug.Log("Back Button was clicked.");
    }
}

Methods

Loads a url in current web view.

Parameters
  • string url

    The url to be loaded. This url should start with http:// or https:// scheme, or retrieved from the path methods in UniWebViewHelper.

  • bool skipEncoding

    Whether UniWebView should skip encoding the url or not. If set to false, UniWebView will try to encode the url parameter before loading it. Otherwise, your original url string will be used as the url if it is valid. Default is false.

  • string readAccessURL

    The URL to allow read access to. This parameter is only used when loading from the filesystem in iOS, and passed to loadFileURL:allowingReadAccessToURL: method of WebKit. By default, the parent folder of the url parameter will be read accessible.

Example

// Load a URL.
webView.Load("https://example.com");

// Load a URL which is already escaped.
webView.Load("https://example.com?email=support%40uniwebview.com", true);

// Load a local file, with "local_app_folder/root/images/" as its read access path.
var indexURL = UniWebViewHelper.StreamingAssetURLForPath("local_app_folder/root/page/index.html");
var accessURL = UniWebViewHelper.StreamingAssetURLForPath("local_app_folder/root/images/");
webView.Load(indexURL, false, accessURL);

Loads an HTML string in current web view.

Parameters
  • string htmlString

    The HTML string to use as the contents of the webpage.

  • string baseUrl

    The url to use as the page's base url.

  • bool skipEncoding

    Whether UniWebView should skip encoding the baseUrl or not. If set to false, UniWebView will try to encode the baseUrl parameter before using it. Otherwise, your original url string will be used as the baseUrl if it is valid. Default is false.

Example

webView.LoadHTMLString("<p>Hello World</p>", "https://domain.com");

Reloads the current page.

Stops loading all resources on the current page.

Navigates to the back item in the back-forward list.

Example

if (webView.CanGoBack) {
    webView.GoBack();
}

Navigates to the forward item in the back-forward list.

Example

if (webView.CanGoForward) {
    webView.GoForward();
}

Sets whether the link clicking in the web view should open the page in an external browser.

By default, when the user clicks a link, it will be opened in the same web view. After setting this with true, the user will be navigated to an external native browser.

On iOS, the mobile Safari; on Android, the default browser like Chrome; on macOS Editor, the default browser of your system will be used.

Parameters
  • bool flag

    The flag indicates whether a link should be opened externally.

Example

// You may want to set it in OnPageFinished event, 
// otherwise the original page will be also opened externally
webView.OnPageFinished += (view, statusCode, url) => {
    webView.SetOpenLinksInExternalBrowser(true);
};

Sets the web view visible on screen.

If you pass false and UniWebViewTransitionEdge.None to the first two parameters, it means no animation will be applied when showing. So the duration parameter will not be taken into account. Otherwise, when either or both fade and edge set, the showing operation will be animated.

Regardless of there is an animation or not, the completionHandler will be called if not null when the web view showing finishes.

Parameters
  • bool fade

    Whether show with a fade in animation. Default is false.

  • UniWebViewTransitionEdge edge

    The edge from which the web view showing. It simulates a modal effect when showing a web view. Default is UniWebViewTransitionEdge.None.

  • float duration

    Duration of the showing animation. Default is 0.4f.

  • Action completionHandler

    Completion handler which will be called when showing finishes. Default is null.

Return Value

A bool value indicates whether the showing operation started.

Example

// Show the web view without animation
webView.Show();

// Show the web view with a fade animation
webView.Show(true);

// Show the web view with a modal presenting animation from screen bottom
webView.Show(false, UniWebViewTransitionEdge.Bottom);

// Print a message after the web view shown with animation
webView.Show(true, UniWebViewTransitionEdge.Top, 0.25f, ()=> {
    print("Show transition finished!");
});

Sets the web view invisible from screen.

If you pass false and UniWebViewTransitionEdge.None to the first two parameters, it means no animation will be applied when hiding. So the duration parameter will not be taken into account. Otherwise, when either or both fade and edge set, the hiding operation will be animated.

Regardless there is an animation or not, the completionHandler will be called if not null when the web view hiding finishes.

NOTICE

Hiding the web view does not destroy or release it. You can always call Show on the web view again to make it visible.

To release a web view and its resource, pass the web view component as the parameter of Destroy.

Parameters
  • bool fade

    Whether hide with a fade in animation. Default is false.

  • UniWebViewTransitionEdge edge

    The edge from which the web view hiding. It simulates a modal effect when hiding a web view. Default is UniWebViewTransitionEdge.None.

  • float duration

    Duration of hiding animation. Default is 0.4f.

  • Action completionHandler

    Completion handler which will be called when hiding finishes. Default is null.

Return Value

A bool value indicates whether the hiding operation started.

Example

// Hide the web view without animation
webView.Hide();

// Hide the web view with a fade animation
webView.Hide(true);

// Hide the web view with a modal presenting animation from screen bottom
webView.Hide(false, UniWebViewTransitionEdge.Bottom);

// Print a message after the web view hidden with animation
webView.Hide(true, UniWebViewTransitionEdge.Top, 0.25f, ()=> {
    print("Hide transition finished!");
});

Animates the web view from current Frame (position and size) to another Frame (position and size) within duration.

Parameters
  • Rect frame

    The new Frame which the web view should be.

  • float duration

    Duration of the animation.

  • float delay

    Delay before the animation begins. Default is 0.0f, which means the animation will start immediately.

  • Action completionHandler

    Completion handler which will be called when animation finishes. Default is null.

Return Value

A bool value indicates whether the animation started.

Example

// Animate current web view to cover half of the screen.
var halfScreen = new Rect(0, 0, Screen.width, Screen.height / 2);
webView.AnimateTo(halfScreen, 0.4f, 0.1f, () => {
    print("Animation finished!");
});

Updates and sets current frame of web view to match the setting.

This is useful if the referenceRectTransform is changed and you need to sync the frame change to the web view. This method follows the frame determining rules.

Example

// In a UIBehavior script:
// Called when associated `rectTransform` is changed.
void OnRectTransformDimensionsChange() {
    // This will update web view's frame to match the reference rect transform if set.
    webView.UpdateFrame();
}

Adds a JavaScript to current page. Normally, you add a JavaScript function or variable with this method.

The input jsString will be executed by current web view. If succeeded, the input JavaScript code will "inject" to the web view and a UniWebViewNativeResultPayload with resultCode being "0" will passed to the completionHandler.

Parameters
  • string jsString

    The JavaScript code to add. It should be a valid JavaScript statement string.

  • Action<UniWebViewNativeResultPayload> completionHandler

    Called when adding JavaScript operation finishes. Default is null. If everything goes fine and the jsString added to current web view, resultCode would be "0"

Example

webView.AddJavaScript("function add() { return 1 + 2; }", (payload)=>{
    if (result.resultCode.Equal("0")) {
        print("JavaScript adding finished without problem.");
    }
});

Evaluates a JavaScript string on current page. Normally you execute a certain JavaScript function or get a variable by this method.

The input jsString will be executed by current web view. Executing result will be sent back to you in the completionHandler. You could access the data member of UniWebViewNativeResultPayload passed in to get the JavaScript function return value.

Parameters
  • string jsString

    The JavaScript string to evaluate.

  • Action<UniWebViewNativeResultPayload> completionHandler

    Called when evaluating JavaScript operation finishes. Default is null. If everything goes find, the resultCode would be "0" and the return value of invoked JavaScript is contained in data.

Example

// Pop up an alert from web view.
webView.EvaluateJavaScript("alert('Alert!');", (payload)=>{
    print(payload.resultCode); // => "0"
    print(payload.data); // => ""
});

// Adding two numbers by a JavaScript function.
webView.AddJavaScript("function add(a, b) { return a + b; }", completionHandler: _ => {
    webView.EvaluateJavaScript("add(4, 5);", completionHandler: (payload) => {
        print(payload.resultCode); // => "0"
        print(payload.data);  // => "9"
    });
});

// Call a JavaScript function not existing.
webView.EvaluateJavaScript("functionNotExisting()", completionHandler: (payload) => {
    print(payload.resultCode);
    // a non-zero value which indicates JavaScript error code.
    // eg. "4" on iOS.
});

Adds a url scheme to UniWebView message system interpreter. All following url navigation to this scheme will be sent as a message to UniWebView instead.

Parameters
  • string scheme

    The URL scheme to add. It should not contain "://" part. You could even add "http" and/or "https" to prevent all resource loading on the page. "uniwebview" is added by default. Nothing will happen if you try to add a duplicated scheme.

Example

// Add "myscheme" as a UniWebView message scheme.
webView.AddUrlScheme("myscheme");

// A link like "myscheme://action" will be treated as a message and raise the `OnMessageReceived` event from now.

Removes a url scheme from UniWebView message system interpreter.

Parameters
  • string scheme

    The url scheme to remove. Nothing will happen if the scheme is not in the message system.

Example

webView.RemoveUrlScheme("myscheme");

Adds a domain to the SSL checking white list.

If you are trying to access a website with un-trusted or expired certification, the web view will prevent its loading. If you could confirm that this site is trusted, you can add the domain as an SSL exception, so you could visit it.

NOTICE

We strongly suggest you upgrade your site certification to a trusted one. It would be dangerous to add a site as SSL exception and your user might be exposed to the risk of Man-in-the-middle attack. You should know exactly what you will do before adding a domain to the whitelist.

Parameters
  • string domain

    The domain to add. It should not contain any scheme or path part in url.

Example

// This loading will fail since the certification is a self-signed one and not trusted.
webView.Load("https://self-signed.badssl.com/"); 

// Add "self-signed.badssl.com" as trusted.
webView.AddSslExceptionDomain("self-signed.badssl.com");
// This page should load now.
webView.Load("https://self-signed.badssl.com/"); 

Removes a domain from the SSL checking white list.

Parameters
  • string domain

    The domain to remove. It should not contain any scheme or path part in url.

Example

webView.RemoveSslExceptionDomain("self-signed.badssl.com");

Sets a customized header field for web view requests.

The header field will be used for all subsequence request. Pass null as value to unset a header field.

Some reserved headers like user agent are not able to override by setting here, use the SetUserAgent method for them instead.

NOTICE

Customized header fields will only be set for GET requests. The header fields set by this method will not be added when a form is submitted as POST requests, due to some limitation of WebKit on iOS and Android platforms.

Parameters
  • string key

    The key of customized header field.

  • string value

    The value of customized header field. null if you want to unset the field.

Example

// Set "MyToken" field to "123abc" in a web view. It will be used for all following requests.
webView.SetHeaderField("MyToken", "123abc");

// Unset it
webView.SetHeaderField("MyToken", null);

Sets the user agent used in the web view. If the string is null or empty, the system default value will be used.

Parameters
  • string agent

    The new user agent string to use.

Example

// Set the user agent string sent in request header.
webView.SetUserAgent("My-App/1.0.0 (iOS 10.3, iPhone 7)");

// => In request header:
// User-Agent = "My-App/1.0.0 (iOS 10.3, iPhone 7)"

Gets the user agent string currently used in web view. If a customized user agent is not set, the default user agent in current platform will be returned.

Return Value

The user agent string in use.

Example

// Gets the default user agent.
webView.GetUserAgent()
// => "Mozilla/5.0 (iPhone; CPU iPhone OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 ..."
// This value varies in different platforms.

// Sets a user agent and then get it.
webView.SetUserAgent("My-App/1.0.0 (iOS 10.3, iPhone 7)");
webView.GetUserAgent()
// => "My-App/1.0.0 (iOS 10.3, iPhone 7)"

Sets the adjustment behavior which indicates how safe area insets are added to the adjusted content inset. It is a wrapper of contentInsetAdjustmentBehavior on iOS.

It only works on iOS 11 and above.

NOTICE

You need to call this method as soon as you create a web view, before you call any other methods related to web view layout (like Show or SetShowToolbar).

Parameters
  • UniWebViewContentInsetAdjustmentBehavior behavior

    The behavior for determining the adjusted content offsets.

Example

var webView = gameObject.AddComponent<UniWebView>();
// Do not adjust the scroll view insets in the web view.
webView.SetContentInsetAdjustmentBehavior(UniWebViewContentInsetAdjustmentBehavior.Never);

Sets allow auto-play for current web view. By default, users need to touch the play button to start playing a media resource.

By setting this to true, you can start the playing automatically through corresponding media tag attributes.

NOTICE

You need to set it before creating a web view. Existing web views are not affected.

Parameters
  • bool flag

    A flag indicates whether auto-playing of media is allowed or not.

Example

UniWebView.SetAllowAutoPlay(true);

// Create a new web view.
var webView = gameObject.AddComponent<UniWebView>();

// Now load and open a page which contains auto-started video to try
webView.Load("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_video_autoplay");
webView.Show();

Sets allow inline play for current web view. By default, on iOS, the video can only be played in a new full screen view.

By setting this to true, you could play a video inline the page, instead of opening a new full-screen window.

This only works for iOS and macOS Editor. On Android, you could play videos inline by default and calling this method does nothing.

Remember you also need to add "playsinline" attribute to your video tag in the HTML page.

NOTICE

You need to set it before creating a web view. Existing web views are not affected.

Parameters
  • bool flag

    A flag indicates whether inline playing of media is allowed or not.

Example

UniWebView.SetAllowInlinePlay(true);

// Create a new web view.
var webView = gameObject.AddComponent<UniWebView>();

// Now load and open a page which contains inline video to try:
// <video src="file.mp4" playsinline> or <video src="file.mp4" webkit-playsinline>
webView.Load("https://example.com/inline-video");
webView.Show();

Sets whether loading a local file is allowed.

If set to false, any load from a file URL file:// for Load method will be rejected and trigger an OnLoadingErrorReceived event. That means you cannot load a web page from any local file. If you are not going to load any local files, setting it to false helps to reduce the interface of web view and improve the security.

By default, it is true and the local file URL loading is allowed.

Parameters
  • bool flag

    Whether the local file access by web view loading is allowed or not.

Example

// Forbid file:// URL loading.
webView.SetAllowFileAccess(false);

// This won't load and trigger an error in OnLoadingErrorReceived.
var url = UniWebViewHelper.StreamingAssetURLForPath("www/index.html");
webView.Load(url);

Sets whether file access from file URLs is allowed.

By setting with true, access to file URLs inside the web view will be enabled and you could access sub-resources or make cross origin requests from local HTML files.

On iOS, it uses some "hidden" way by setting allowFileAccessFromFileURLs in config preferences for WebKit. So it is possible that it stops working in a future version.

On Android, it sets the WebSettings.setAllowFileAccessFromFileURLs for the current web view.

NOTICE

By setting this to true, you will bring some potential security issue to your app. Some malicious script would be able to read your sandbox. So we DO NOT recommend to enable it before you realize and understand the risk.

Parameters
  • bool flag

    Whether the file access inside web view from file URLs is allowed or not.

Example

webView.SetAllowFileAccessFromFileURLs(true);

Sets whether the UniWebView should allow third party cookies to be set.

By default, on Android, the third party cookies are disallowed due to security reason. Setting this to true will allow the cookie manager to accept third party cookies you set.

This method only works for Android. On iOS, this method does nothing and the third party cookies are always allowed.

Parameters
  • bool flag

    Whether the third party cookies should be allowed.

Sets allow universal access from file URLs. By default, on iOS, the WKWebView forbids any load of local files through AJAX even when opening a local HTML file. It checks the CORS rules and fails at web view level. This is useful when you want access these files by setting the allowUniversalAccessFromFileURLs key of web view configuration.

On iOS and macOS Editor. It uses some "hidden" way by setting allowUniversalAccessFromFileURLs in config for WebKit. So it is possible that it stops working in a future version.

On Android, it sets the WebSettings.setAllowUniversalAccessFromFileURLs and any later-created web views uses that value.

NOTICE

You need to set it before creating a web view. Existing web views are not affected.

By setting this to true, you will bring some potential security issue to your app. Some malicious script would be able to read your sandbox. So we DO NOT recommend to enable it before you realize and understand the risk.

Parameters
  • bool flag

    A flag indicates whether the universal access for files are allowed or not.

Example

UniWebView.SetAllowUniversalAccessFromFileURLs(true);

// Create a new web view.
var webView = gameObject.AddComponent<UniWebView>();

Sets whether the web view area should avoid soft keyboard. If true, when the keyboard shows up, the web views content view will resize itself to avoid keyboard overlap the web content. Otherwise, the web view will not resize and just leave the content below under the soft keyboard.

Default is true.

NOTICE

This method is only for Android. On iOS, the keyboard avoidance is built into the system directly and there is no way to change its behavior.

Parameters
  • bool flag

    Whether the keyboard should avoid web view content.

Example

UniWebView.SetEnableKeyboardAvoidance(false);

// Create a new web view.
var webView = gameObject.AddComponent<UniWebView>();

Sets whether JavaScript should be enabled in current web view. Default is enabled.

For a modern page, you may always want JavaScript enabled. However, if you could confirm that you are not using any JavaScript in your page, you could turn it off to get better performance and safety.

NOTICE

You need to set it before creating a web view. Existing web views are not affected.

Parameters
  • bool enabled

    Whether JavaScript should be enabled.

Example

// Disable JavaScript in web views created later.
UniWebView.SetJavaScriptEnabled(false);

// JavaScript is disabled in this web view.
var webView = gameObject.AddComponent<UniWebView>();

Sets whether the web view limits navigation to pages within the app’s domain.

This only works on iOS 14.0+. For more information, refer to the Apple's documentation and the App-Bound Domains page. This requires additional setup in WKAppBoundDomains key in the Info.plist file.

On Android, this method does nothing.

NOTICE

You need to set it before creating a web view. Existing web views are not affected.

Parameters
  • bool enabled

    Whether limiting navigation should be enabled.

Example

// Enable navigation limitation in web views created later.
UniWebView.SetLimitsNavigationsToAppBoundDomains(true);

// This config is applied in this web view.
var webView = gameObject.AddComponent<UniWebView>();

Sets whether JavaScript can open windows without user interaction.

By setting this to true, an automatically JavaScript navigation will be allowed in the web view.

NOTICE

You need to set it before creating a web view. Existing web views are not affected.

Parameters
  • bool flag

    Whether JavaScript could open window automatically.

Example

// Allow JavaScript open window automatically.
UniWebView.SetAllowJavaScriptOpenWindow(true);

var webView = gameObject.AddComponent<UniWebView>();

// Now, the following JavaScript code could navigate to 
// "example.com" without user interaction in `webView`.
setTimeout(function() {
    window.location.href = 'https://example.com';
}, 300);

Sets whether a callout (context) menu should be displayed when user long tapping on certain web view content.

When enabled, when user long presses an image or link in the web page, a context menu would be show up to ask user's action. On iOS, it is a action sheet to ask whether opening the target link or saving the image. On Android it is a pop up dialog to ask whether saving the image to local disk. On iOS, the preview page triggered by force touch on iOS is also considered as a callout menu.

Default is true, means that the callout menu will be displayed. Call this method with false to disable it on the web view.

Parameters
  • bool enabled

    Whether a callout menu should be displayed when user long pressing or force touching a certain web page element.

Example

// Disable callout menu in the web view.
webView.SetCalloutEnabled(false);

Sets whether the web view should support a pop up web view triggered by user in a new tab.

In a general web browser (such as Google Chrome or Safari), a URL with target="_blank" attribute is intended to be opened in a new tab. However, in the context of web view, there is no way to handle new tabs without proper configurations. Due to that, by default UniWebView will ignore the target="_blank" and try to open the page in the same web view if that kind of link is pressed.

It works for most cases, but if this is a problem to your app logic, you can change this behavior by calling this method with true. It enables the "opening in new tab" behavior in a limited way, by adding the new tab web view above to the current web view, with the same size and position. When the opened new tab is closed, it will be removed from the view hierarchy automatically.

By default, only user triggered action is allowed to open a new window for security reason. That means, if you are using some JavaScript like window.open, unless you set allowJavaScriptOpening to true, it won't work. This default behavior prevents any other third party JavaScript code from opening a window arbitrarily.

Parameters
  • bool enabled

    Whether to support multiple windows. If true, the target="_blank" link will be opened in a new web view. Default is false.

  • bool allowJavaScriptOpening

    Whether to support open the new window with JavaScript by window.open. Setting this to true means any JavaScript code, even from third party (in an iframe or a library on the page), can open a new window. Use it as your risk.

    Usually, when passing true to this parameter, you may also want to call SetAllowJavaScriptOpenWindow with true before creating the web view.

Sets the default font size used in the web view.

On Android, the web view font size can be affected by the system font scale setting. Use this method to set the font size in a more reasonable way, by giving the web view another default font size with the system font scale considered. It can removes or reduces the effect of system font scale when displaying the web content.

This method only works on Android. On iOS, this method does nothing since the web view will respect the font size setting in your CSS styles.

Parameters
  • int size

    The target default font size set to the web view.

Sets the text zoom used in the web view.

On Android, this method call WebSettings.setTextZoom to the the text zoom used in the web view.

This method only works on Android.

Parameters
  • int textZoom

    The text zoom in percent.

Sets whether the drag interaction should be enabled on iOS.

From iOS 11, the web view on iOS supports the drag interaction when user long presses an image, link or text. Setting this to false would disable the drag feather on the web view.

On Android, there is no direct native way to disable drag only. This method instead disables the long touch event, which is used as a trigger for drag interaction. So, setting this to false would disable the long touch gesture as a side effect.

It does nothing on macOS editor. Default is true, which means drag interaction is enabled if the device and system version supports it.

Parameters
  • bool enabled

    Whether the drag interaction should be enabled.

Example

// Disable the drag interaction in the web view.
webView.SetDragInteractionEnabled(false);

Sets the way of how the cache is used when loading a request.

This controls the loading behavior of whether the cache should be used or how to use it for the first request of the Load method. The default value is UniWebViewCacheMode.Default. See the UniWebViewCacheMode enum for more.

Parameters
  • UniWebViewCacheMode mode

    The desired cache mode that the request loading should be used in this web view.

Example

// Set the cache mode to only load from cache but not from network.
// If no cache is available, the loading will fail.
webView.SetCacheMode(UniWebViewCacheMode.CacheOnly);
webView.Load("https://example.com");

Cleans web view cache. This removes cached local data of web view.

If you need to clear all cookies, use ClearCookies instead.

Sets whether the web page console output should be forwarded to native console.

By setting this to true, UniWebView will try to intercept the web page console output methods and forward them to the native console, for example, Xcode console on iOS, Android logcat on Android and Unity Console when using Unity Editor on macOS. It provides a way to debug the web page by using the native console without opening the web inspector. The forwarded logs in native side contains a "<UniWebView-Web>" tag.

Logs from the methods below will be forwarded:

  • console.log
  • console.warn
  • console.error
  • console.debug

NOTICE

Default is false. You need to set it before you create a web view instance to apply this setting. Any existing web views are not affected.

Parameters
  • bool flag

    Whether the web page console output should be forwarded to native output.

Example

// Do it before you create the web view.
UniWebView.SetForwardWebConsoleToNativeOutput(true);

// Create a new web view.
var webView = gameObject.AddComponent<UniWebView>();
// ...

// Later, in the web page JavaScript.
function sayHello() {
  console.log("Hello from web page!");
}

// "Hello from web page!" will be printed in the native console.

Sets a cookie for a certain url.

The cookie string supports all available cookie properties as well as multiple cookies. See

UniWebView respects the server cookie header by default. Generally, you do not need to set the cookie from client manually. However, if you have to pass your server a manually set cookie, use this method.

Parameters
  • string url

    The url to which cookie will be set.

  • string cookie

    The cookie string to set. Need more information on cookie? See the HTTP cookie page.

  • bool skipEncoding

    Whether UniWebView should skip encoding the url or not. If set to false, UniWebView will try to encode the url parameter before using it. Otherwise, your original url string will be used to set the cookie if it is valid. Default is false.

Example

// Set a cookie "testCookie=1" to current url/domain.
UniWebView.SetCookie(webView.Url, "testCookie=1");

// Set a full properties specified cookie.
UniWebView.SetCookie("https://example.com", "sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT");

Gets the cookie value under a url and key.

Parameters
  • string url

    The url (domain) where the target cookie is.

  • string key

    The key for target cookie value.

  • bool skipEncoding

    Whether UniWebView should skip encoding the url or not. If set to false, UniWebView will try to encode the url parameter before using it. Otherwise, your original url string will be used to get the cookie if it is valid. Default is false.

Return Value

Value of the target cookie under url.

Example

UniWebView.GetCookie("https://example.com", "testCookie");
// => The corresponding cookie value. Or "" if there is no such cookie.

Removes all the cookies under a url.

Parameters
  • string url

    The url (domain) where the target cookie is.

  • bool skipEncoding

    Whether UniWebView should skip encoding the url or not. If set to false, UniWebView will try to encode the url parameter before using it. Otherwise, your original url string will be used to get the cookie if it is valid. Default is false.

Example

UniWebView.RemoveCookies("https://example.com");

Removes the certain cookie under a url for the specified key.

Parameters
  • string url

    The url (domain) where the target cookie is.

  • string key

    The key for target cookie.

  • bool skipEncoding

    Whether UniWebView should skip encoding the url or not. If set to false, UniWebView will try to encode the url parameter before using it. Otherwise, your original url string will be used to get the cookie if it is valid. Default is false.

Example

UniWebView.RemoveCookie("https://example.com", "SID");

Clear all cookies from web views.

NOTICE

This will clear cookies from all domains in the web view and previous. If you only need to remove cookies from a certain domain, use RemoveCookies instead.

Example

UniWebView.ClearCookies();

Clears any saved credentials for HTTP authentication for both Basic and Digest.

On both iOS and Android, the user input credentials will be stored permanently across the session. It could prevent your users to input username and password again until they changed. If you need the credentials only living in a shorter lifetime, call this method at proper timing.

On iOS, it will clear the credentials immediately and completely from both disk and network cache. On Android, it only clears from disk database, the authentication might be still cached in the network stack and will not be removed until next session (app restarting).

The client logout mechanism should be implemented by the Web site designer (such as server sending an HTTP 401 for invalidating credentials).

Parameters
  • string host

    The host to which the credentials apply. It should not contain any thing like scheme or path part.

  • string realm

    The realm to which the credentials apply.

Example

UniWebView.ClearHttpAuthUsernamePassword("uniwebview.com", "UniWebViewUserRealm");

Sets whether to show a loading indicator while the loading is in progress.

Parameters
  • bool flag

    Whether an indicator should show.

Sets the text displayed in the loading indicator, if SetShowSpinnerWhileLoading is set to true.

Parameters
  • string text

    The text to display while loading indicator visible. Default is "Loading..."

Sets whether the user can dismiss the loading indicator by tapping on it or the greyed-out background around.

By default, when the loading spinner is shown, the user can dismiss it by a single tapping. Call this method with false to disable this behavior.

Parameters
  • bool flag

    Whether the user can dismiss the loading indicator.

Shows the loading spinner.

Calling this method will show the loading spinner, regardless if the SetShowSpinnerWhileLoading is set to true or false. However, if SetShowSpinnerWhileLoading was called with true, UniWebView will still hide the spinner when the loading finishes.

Hides the loading spinner.

Calling this method will hide the loading spinner, regardless if the SetShowSpinnerWhileLoading is set to true or false. However, if SetShowSpinnerWhileLoading was called with false, UniWebView will still show the spinner when the loading starts.

Sets whether the horizontal scroll bar should show when the web content beyonds web view bounds.

This only works on mobile platforms. It will do nothing on macOS Editor.

Parameters
  • bool enabled

    Whether enable the scroll bar or not.

Sets whether the vertical scroll bar should show when the web content beyonds web view bounds.

This only works on mobile platforms. It will do nothing on macOS Editor.

Parameters
  • bool enabled

    Whether enable the scroll bar or not.

Sets whether the web view should show with a bounces effect when scrolling to page edge.

This only works on mobile platforms. It will do nothing on macOS Editor.

Parameters
  • bool enabled

    Whether enable the bounces effect should be applied or not.

Sets whether the web view supports zoom gesture to change content size. Default is false, which means the zoom gesture is not supported.

Parameters
  • bool enabled

    Whether the zoom gesture is allowed or not.

Adds a trusted domain to white list and allow permission requests from the domain.

You need this on Android devices when a site needs the location or camera permission. It will allow the permission gets approved so you could access the corresponding devices.

NOTICE

Deprecated. Use RegisterOnRequestMediaCapturePermission instead, which works for both Android and iOS. It also provides a more flexible way to handle the permission requests. By default, if neither this method and RegisterOnRequestMediaCapturePermission is called, the permission request will trigger a grant alert to ask the user to decide whether to allow or deny the permission.

Parameters
  • string domain

    The domain to add to the white list.

Example

// Deprecated. Do not do it anymore.
webView.AddPermissionTrustDomain("my-own-site.com");

// Instead, use this:
webView.RegisterOnRequestMediaCapturePermission((request) => {
    if (request.Host == "my-own-site.com") {
        return UniWebViewMediaCapturePermissionDecision.Grant;
    } else {
        return UniWebViewMediaCapturePermissionDecision.Prompt;
    }
});

Removes a trusted domain from white list.

NOTICE

Deprecated. If you previously registered a media capture permission request handler through RegisterOnRequestMediaCapturePermission, use UnregisterOnRequestMediaCapturePermission to cancel it.

Parameters
  • string domain

    The domain to remove from white list.

Sets whether the device back button should be enabled to execute "go back" or "closing" operation.

On Android, the device back button in navigation bar will navigate users to a back page. If there is no any back page available, the back button clicking will try to raise an OnShouldClose event and try to close the web view if true is return from the event. If the OnShouldClose event is not listened to, the web view will be closed and the UniWebView component will be destroyed to release any resource in use.

Listen to OnKeyCodeReceived if you need to disable the back button, but still, want to get the back button key pressing event.

This method is only for Android. On iOS, you could show a toolbar with navigation and Done buttons for similar purpose or call SetAllowBackForwardNavigationGestures to enable gesture based navigation on iOS.

The default is enabled.

Parameters
  • bool enabled

    Whether the back button should perform go back or closing operation to web view.

Sets whether the web view should enable support for the "viewport" HTML meta tag or should use a wide viewport.

This method is only for Android. The default is disabled.

Parameters
  • bool flag

    Whether to enable support for the viewport meta tag.

Sets whether the web view loads pages in overview mode, that is, zooms out the content to fit on screen by width.

This method is only for Android. The default is disabled.

Parameters
  • bool flag

    Whether to enable support for the overview mode.

Sets whether the web view should behave in immersive mode, that is, hides the status bar and navigation bar with a sticky style.

If disabled, the navigation bar will always show up while the web view is visible.

This method is only for Android. The default is enabled.

Parameters
  • bool enabled

    Whether to enable immersive mode when web view is showing up.

Sets whether to show a toolbar which contains navigation buttons and Done button.

You could choose to show or hide the toolbar. By configuring the animated and onTop parameters, you can control the animating and position of the toolbar. If the toolbar is overlapping with some part of your web view, pass adjustInset with true to have the web view relocating itself to avoid the overlap.

This method is only for iOS. The toolbar is hidden by default.

NOTICE

This method is deprecated. Use EmbeddedToolbar.Show() or EmbeddedToolbar.Hide() instead. See EmbeddedToolbar for more information.

Parameters
  • bool show

    Whether the toolbar should show or hide.

  • bool animated

    Whether the toolbar state changing should be with animation. Default is false.

  • bool onTop

    Whether the toolbar should snap to top of screen or to bottom of screen. Default is true

  • bool adjustInset

    Whether the toolbar transition should also adjust web view position and size if overlapped. Default is false

Example

// Show a toolbar at top of screen with animation.
webView.SetShowToolbar(true, true, true);

// Hide the tool bar without animation. At the same time, make 
// the web view snap to screen top if there was overlapping between 
// the web view and toolbar.
webView.SetShowToolbar(false, false, true, true);

Sets the done button text in toolbar.

By default, UniWebView will show a "Done" button at bottom-right corner in the toolbar. You could change its title by passing a text.

This method is only for iOS since there is no toolbar on Android.

NOTICE

This method is deprecated. Use EmbeddedToolbar.SetDoneButtonText instead. See EmbeddedToolbar for more information.

Parameters
  • string text

    The text needed to be set as done button title.

Example

webView.SetToolbarDoneButtonText("关闭");

Sets the go back button text in toolbar.

By default, UniWebView will show a back arrow at the left side in the toolbar. You could change its text.

This method is only for iOS and macOS Editor, since there is no toolbar on Android.

NOTICE

This method is deprecated. Use EmbeddedToolbar.SetGoBackButtonText instead. See EmbeddedToolbar for more information.

Parameters
  • string text

    The text needed to be set as go back button.

Example

webView.SetToolbarGoBackButtonText("返回");

Sets the go forward button text in toolbar.

By default, UniWebView will show a forward arrow at the left side in the toolbar. You could change its text.

This method is only for iOS and macOS Editor, since there is no toolbar on Android.

NOTICE

This method is deprecated. Use EmbeddedToolbar.SetGoForwardButtonText instead. See EmbeddedToolbar for more information.

Parameters
  • string text

    The text needed to be set as go forward button.

Example

webView.SetToolbarGoForwardButtonText("前进");

Sets the visibility of navigation buttons, such as "Go Back" and "Go Forward", on toolbar.

By default, UniWebView will show the "Go Back" and "Go Forward" navigation buttons on the toolbar. Users can use these buttons to perform go back or go forward action just like in a browser. If the navigation model is not for your case, call this method with false as show parameter to hide them.

This method is only for iOS, since there is no toolbar on Android.

NOTICE

This method is deprecated. Use EmbeddedToolbar.ShowNavigationButtons or EmbeddedToolbar.HideNavigationButtons instead. See EmbeddedToolbar for more information.

Parameters
  • bool show

    Whether the navigation buttons on the toolbar should show or hide.

Sets the background tint color for the toolbar.

By default, UniWebView uses a default half-transparent iOS standard background for toolbar. You can change it by setting a new opaque color.

This method is only for iOS, since there is no toolbar on Android.

NOTICE

This method is deprecated. Use EmbeddedToolbar.SetBackgroundColor instead. See EmbeddedToolbar for more information.

Parameters
  • Color color

    The color should be used for the background tint of the toolbar.

Sets the button text color for the toolbar.

By default, UniWebView uses the default text color on iOS, which is blue for most cases. You can change it by setting a new opaque color.

This method is only for iOS, since there is no toolbar on Android.

NOTICE

This method is deprecated. Use EmbeddedToolbar.SetButtonTextColor or EmbeddedToolbar.SetTitleTextColor instead. See EmbeddedToolbar for more information.

Parameters
  • Color color

    The color should be used for the button text of the toolbar.

Sets whether the web view can receive user interaction or not.

By setting this to false, the web view will not accept any user touch event so your users cannot tap links or scroll the page.

Parameters
  • bool enabled

    Whether the user interaction should be enabled or not.

Sets whether the web view should pass through clicks at its clear pixels to Unity scene.

Setting this method is a pre-condition for the whole passing-through feature to work. To allow your touch passing through to Unity scene, the following conditions should be met at the same time:

  1. This method is called with true. It enables the web view to check every touch on the web view.
  2. The web view has a transparent background in body style for its content by CSS.
  3. The web view itself has a transparent background color by setting BackgroundColor with a clear color.

Then, when user clicks on the clear pixel on the web view, the touch events will not be handled by the web view. Instead, these events are passed to Unity scene. By using this feature, it is possible to create a UI for your game with the web view.

Only clicks on transparent part on the web view will be delivered to Unity scene. The web view still intercepts and handles other touches on visible pixels on the web view.

Parameters
  • bool enabled

    Whether the transparency clicking through feature should be enabled in this web view.

Example

// Allow transparency clicking through.
webView.SetTransparencyClickingThroughEnabled(true);

// Make Unity scene visible.
webView.BackgroundColor = Color.clear;

// Disable the scrolling bounces effect to fix the web UI.
webView.SetBouncesEnabled(false);

// Other configuration, usually handle some messages from web view.
webView.OnMessageReceived += (view, message) => {
    // ...
};

Enables debugging of web contents. You could inspect of the content of a web view by using a browser development tool of Chrome for Android or Safari for macOS.

This method is only for Android and macOS Editor. On iOS, you do not need an additional step. You could open Safari's developer tools to debug a web view on iOS.

NOTICE

Due to a memory bug under WebKit and Unity, it might crash your macOS Editor when you stop playing with an inspector showing embedded in a web view. You could close the inspector first or use it as a standalone window to avoid this. It will only happen in the editor and never affect real devices.

Please remember to disable this in your product build. This should be only used while development.

Parameters
  • bool enabled

    Whether the content debugging should be enabled.

Enables user resizing for web view window. By default, you can only set the window size by setting its frame on mac Editor. By enabling user resizing, you would be able to resize the window by dragging its border as a normal macOS window.

NOTICE

This method only works for macOS for debugging purpose. It does nothing on iOS and Android.

Parameters
  • bool enabled

    Whether the window could be able to be resized by the cursor.

Sets whether horizontal swipe gestures should trigger back-forward list navigation.

By setting with true, users can swipe from screen edge to perform a back or forward navigation. This method only works on iOS and macOS Editor. Default is false.

On Android, the screen navigation gestures are simulating the traditional back button and it is enabled by default. To disable gesture navigation on Android, you have to also disable the device back button. See SetBackButtonEnabled for that purpose.

Parameters
  • bool flag

    The value indicates whether a swipe gestures driven navigation should be allowed. Default is false.

Example

webView.SetAllowBackForwardNavigationGestures(true);

Sets whether a prompt alert should be displayed for collection username and password when the web view receives an HTTP authentication challenge (HTTP Basic or HTTP Digest) from server.

By setting with false, no prompt will be shown and the user cannot login with input credentials. In this case, you can only access this page by providing username and password through the URL like: "http://username:password@example.com". If the username and password does not match, normally an error with 401 as status code would be returned (this behavior depends on the server implementation). If set with true, a prompt will be shown when there is no credentials provided or it is not correct in the URL.

Default is true.

Parameters
  • bool flag

    Whether a prompt alert should be shown for HTTP authentication challenge or not.

Example

// This URL requires HTTP Basic authentication.
var url = "https://example.com/auth/http-basic";

// A prompt alert will be shown and user has a chance to input their username/password.
webView.Load(url);

// Setting to false, this will use "username" and "password" to response server challenge.
webView.SetAllowHTTPAuthPopUpWindow(false);
webView.Load("https://username:password@example.com/auth/http-basic");

// Loading a URL but not providing credentials and no chance for user to input.
// An error might be raised according to your server implementation.
webView.SetAllowHTTPAuthPopUpWindow(false);
webView.Load(url);

Gets the HTML content from current page by accessing its outerHTML with JavaScript.

Parameters
  • Action<string> handler

    Called after the JavaScript executed. The parameter string is the content read from page.

Example

webView.GetHTMLContent((content)=>{
    print(content);
    // => "<html><head> ... </html>"
});
void Print()
iOS
Android

Prints current page.

By calling this method, a native print preview panel will be brought up on iOS and Android. This method does nothing on macOS editor.

NOTICE

On iOS and Android, the web view does not support JavaScript (window.print()), you can only initialize a print job from Unity by this method.

Capture the content of web view and store it to the cache path on disk with the given file name.

When the capturing finishes, OnCaptureSnapshotFinished event will be raised, with an error code to indicate whether the operation succeeded and an accessible disk path of the image.

The captured image will be stored as a PNG file under the fileName in app's cache folder. If a file with the same file name already exists, it will be overridden by the new captured image.

Parameters
  • string fileName

    The file name to which the captured image is stored to, for example "screenshot.png". If empty, UniWebView will pick a random UUID with "png" file extension as the file name.

Example

webView.OnCaptureSnapshotFinished += (view, errorCode, filePath) => {
    if (errorCode != 0) { return; } 
    byte[] bytes = File.ReadAllBytes(filePath);
    Texture2D texture = new Texture2D(2, 2, TextureFormat.RGB24, false);
    texture.LoadImage(bytes);

    // Use the texture.
};
webView.CaptureSnapshot("sample.png");

Starts the process of continually rendering the snapshot.

You take the responsibility of calling this method before you use either GetRenderedData or CreateRenderedTexture(Rect?) to get the rendered data or texture. It prepares a render buffer for the image data and performs the initial rendering for later use.

NOTICE

If this method is not called, the related data or texture methods will not work and will only return null. Once you no longer need the web view to be rendered as a texture, you should call StopSnapshotForRendering to clean up the associated resources.

Parameters
  • Rect? rect

    The optional rectangle to specify the area for rendering. If null (by default), the entire view is rendered.

  • Action<Texture> onStarted

    An optional callback to execute when rendering has started. The callback receives a Texture2D parameter representing the rendered texture. This is useful if you want to perform a one-time rendering of the web view.

Stops the process of continually rendering the snapshot.

You should call this method when you no longer need any further data or texture from the GetRenderedData or CreateRenderedTexture methods. This helps in releasing resources and terminating the rendering process.

Gets the data of the rendered image for the current web view.

This method provides you with the raw bytes of the rendered image data in PNG format. To successfully retrieve the current rendered data, you should first call StartSnapshotForRendering to initiate the rendering process. If StartSnapshotForRendering has not been called, this method will return null.

The rendering area specified by the rect parameter is based on the local coordinates of the web view. For example, new Rect(webView.frame.width / 2, webView.frame.height / 2, 100, 100) means setting the origin to the center of the web view and taking a 100x100 square as the snapshot area.

NOTICE

Please note that this method supports only software-rendered content. Content rendered by hardware, such as videos and WebGL content, will appear as a black rectangle in the rendered image.

Parameters
  • Rect? rect

    The desired rectangle within which the snapshot rendering should occur in the web view. If default value null is used, the whole web view frame will be used as the snapshot area.

Return Value

An array of raw bytes representing the rendered image data in PNG format, or null if the rendering process fails or if the data is not prepared.

Creates a rendered texture for the current web view.

You should destroy the returned texture using the Destroy method when you no longer need it to free up resources.

This method provides you with a texture of the rendered image for the web view, which you can use in your 3D game world. To obtain the current rendered data, you should call StartSnapshotForRendering before using this method. If StartSnapshotForRendering has not been called, this method will return null.

This method returns a plain Texture2D object. The texture is not user interactive and can only be used for display purposes. It is your responsibility to call the Destroy method on this texture when you no longer need it.

Check the Render as Texture guide for more.

NOTICE

You need to destroy the returned texture when you do not need it anymore.

Please note that this method supports only software-rendered content. Content rendered by hardware, such as videos and WebGL content, will appear as a black rectangle in the rendered image.

Parameters
  • Rect? rect

    The desired rectangle within which the snapshot rendering should occur in the web view. If default value null is used, the whole web view frame will be used as the snapshot area.

Return Value

A rendered texture of the current web view, or null if the rendering process fails or the data is not prepared.

Scrolls the web view to a certain point.

Use 0 for both x and y value to scroll the web view to its origin. In a normal vertical web page, it is equivalent as scrolling to top.

You can use the animated parameter to control whether scrolling the page with or without animation. This parameter only works on iOS and Android. On macOS editor, the scrolling always happens without animation.

Parameters
  • int x

    X value of the target scrolling point.

  • int y

    Y value of the target scrolling point.

  • bool animated

    If true, the scrolling happens with animation. Otherwise, it happens without animation and the content is set directly.

Example

// Scroll the web page to top with animation.
webView.ScrollTo(0, 0, true);

Adds the URL to download inspecting list.

If a response is received in main frame and its URL is already in the inspecting list, a download task will be triggered. Check "Download Files" guide for more.

This method only works on iOS and macOS Editor.

Parameters
  • string urlString

    The inspected URL string or a regular expression.

  • UniWebViewDownloadMatchingType type

    The download matching type used to match the URL. Default is ExactValue.

Example

// On iOS, access to "https://example.com/file.pdf" will trigger a download task instead of being rendered in place.
webView.AddDownloadURL("https://example.com/file.pdf");

// You can also use a regular expression.
webView.AddDownloadURL("^https://example\.com/file\.pdf.*$", UniWebViewDownloadMatchingType.RegularExpression);

Removes the URL from download inspecting list.

If a response is received in main frame and its URL is already in the inspecting list, a download task will be triggered. Check "Download Files" guide for more.

This method only works on iOS and macOS Editor.

Parameters
  • string urlString

    The inspected URL string or a regular expression.

  • UniWebViewDownloadMatchingType type

    The download matching type used to match the URL. Default is ExactValue.

Example

// On iOS, access to "https://example.com/file.pdf" will be rendered in place.
webView.RemoveDownloadURL("https://example.com/file.pdf");

Adds the MIME type to download inspecting list.

If a response is received in main frame and its MIME type is already in the inspecting list, a download task will be triggered. Check "Download Files" guide for more.

This method only works on iOS and macOS Editor.

Parameters
  • string MIMEType

    The inspected MIME type of the response.

  • UniWebViewDownloadMatchingType type

    The download matching type used to match the MIME type. Default is ExactValue

Example

// On iOS, access to any PDF files will trigger a download task instead of being rendered in place.
webView.AddDownloadMIMEType("application/pdf");

// You can also use a regular expression.
webView.AddDownloadMIMEType("^image/.*$", UniWebViewDownloadMatchingType.RegularExpression);

Removes the MIME type from download inspecting list.

If a response is received in main frame and its MIME type is already in the inspecting list, a download task will be triggered. Check "Download Files" guide for more.

This method only works on iOS and macOS Editor.

Parameters
  • string MIMEType

    The inspected MIME type of the response.

  • UniWebViewDownloadMatchingType type

    The download matching type used to match the MIME type. Default is ExactValue

Example

// On iOS, access to any PDF files will be rendered in place.
webView.RemoveDownloadMIMETypes("application/pdf");

Sets whether allowing users to edit the file name before downloading. Default is true.

If true, when a download task is triggered, a dialog will be shown to ask user to edit the file name and the user has a chance to choose if the they actually want to download the file. If false, the file download will start immediately without asking user to edit the file name. The file name is generated by guessing from the content disposition header and the MIME type. If the guessing of the file name fails, a random string will be used.

Parameters
  • bool allowed

    Whether the user can edit the file name and determine whether actually starting the downloading.

Sets whether allowing users to choose the way to handle the downloaded file. Default is true.

On iOS, the downloaded file will be stored in a temporary folder. Setting this to true will show a system default share sheet and give the user a chance to send and store the file to another location (such as the File app or iCloud).

On macOS Editor, setting this to true will allow UniWebView to open the file in Finder.

This method does not have any effect on Android. On Android, the file is downloaded to the app's Download folder.

Parameters
  • bool allowed

    Whether the user can choose the way to handle the downloaded file.

Sets whether the OnFileDownloadStarted and OnFileDownloadFinished events should be raised even for an image saving action triggered by the callout (context) menu on Android.

By default, the image saving goes through a different route and it does not trigger the OnFileDownloadStarted and OnFileDownloadFinished events like other normal download tasks. Setting this with enabled with true if you also need to get notified when user long-presses on the image and taps "Save Image" button. By default, the image will be saved to the Downloads directory and you can get the path from the parameter of OnFileDownloadFinished event.

NOTICE

This only works on Android. On iOS, there is no way to get a callback or any event from the "Add to Photos" button in the callout menu.

Parameters
  • bool enabled

    Whether the context menu image saving action triggers the download related events.

Example

webView.SetDownloadEventForContextMenuEnabled(true);

Registers a method handler for deciding whether UniWebView should handle the request received by the web view.

The handler is called before the web view actually starts to load the new request. You can check the request properties, such as the URL, to decide whether UniWebView should continue to handle the request or not. If you return true from the handler function, UniWebView will continue to load the request. Otherwise, UniWebView will stop the loading.

Parameters
  • Func<UniWebViewChannelMethodHandleRequest, bool> handler

    A handler you can implement your own logic against the input request value. You need to return a boolean value to indicate whether UniWebView should continue to load the request or not as soon as possible.

Example

webView.RegisterShouldHandleRequest(request => {
  // Stop loading if the URL contains `example.com`.
  if (request.Url.Contains("example.com")) {
    return false;
  }

  // For other URLs, return `true` and UniWebView can continue to load as usual.
  return true;
});

Unregisters the method handler for handling request received by the web view.

This clears the handler registered by RegisterHandlingRequest method.

Registers a method handler for deciding whether UniWebView should allow a media request from the web page or not.

The handler is called when the web view receives a request to capture media, such as camera or microphone. It usually happens when the web view is trying to access the camera or microphone by using the "getUserMedia" APIs in WebRTC. You can check the request properties in the input UniWebViewChannelMethodMediaCapturePermission instance, which contains information like the media type, the request origin (protocol and host), then decide whether this media request should be allowed or not.

According to the UniWebViewMediaCapturePermissionDecision value you return from the handler function, UniWebView behaves differently:

  • Grant: UniWebView allows the access without asking the user.
  • Deny: UniWebView forbids the access and the web page will receive an error.
  • Prompt: UniWebView asks the user for permission. The web page will receive a prompt to ask the user if they allow the access to the requested media resources (camera or/and microphone).

If this method is never called or the handler is unregistered, UniWebView will prompt the user for the permission.

On iOS, this method is available from iOS 15.0 or later. On earlier version of iOS, the handler will be ignored and the web view will always prompt the user for the permission.

Parameters
  • Func<UniWebViewChannelMethodMediaCapturePermission, UniWebViewMediaCapturePermissionDecision> handler

    A handler you can implement your own logic to decide whether UniWebView should allow, deny or prompt the media resource access request.

    The parameter of the handler is a value of UniWebViewChannelMethodMediaCapturePermission, which contains information like the media type, the request origin (protocol and host).

    You need to return a UniWebViewMediaCapturePermissionDecision value to indicate the decision as soon as possible.

Example

using System.Linq;

webView.RegisterOnRequestMediaCapturePermission((permission) => {
    // Allow the access without asking, if it is from "https://my-domain.com", and only trying to access the camera.
    string[] expected = {"VIDEO"};
    if (permission.Protocol == "https" && 
        permission.Host == "my-domain.com" && 
        permission.Resources.SequenceEqual(expected)
    ) {
        return UniWebViewMediaCapturePermissionDecision.Grant;
    } else {
        // Otherwise, prompt the user for the permission. (This is also the default behavior.)
        return UniWebViewMediaCapturePermissionDecision.Prompt;
    }
});

Unregisters the method handler for handling media capture permission request.

This clears the handler registered by RegisterOnRequestMediaCapturePermission method.