UniWebViewMessage

Summary

Represents a message sent from web content back to Unity. Whenever you want to send some information from web view and handle it in Unity script, you can navigate the user with a link started with "uniwebview://". OnMessageReceived event will be fired for the web view with a UniWebViewMessage object. You can get the URL path and arguments from this message object.

Properties Summary

Gets the raw message.

The url scheme of this UniWebViewMessage.

The path of this UniWebViewMessage.

The arguments of this UniWebViewMessage.

Methods Summary

Initializes a new instance of the UniWebViewMessage struct.

Properties

Gets the raw message. It is the original url which initialized this message.

Example

webView.OnMessageReceived += (view, message) => {
    print(message.RawMessage);
};
webView.Load("uniwebview://action?key=value&anotherKey=value");

// => "uniwebview://action?key=value&anotherKey=value"

The url scheme of this UniWebViewMessage. "uniwebview" was added to message scheme list by default. You can add your own scheme by using UniWebView.AddUrlScheme.

Example

webView.OnMessageReceived += (view, message) => {
    print(message.Scheme);
};
webView.Load("uniwebview://action?key=value&anotherKey=value");

// => "uniwebview"

// Use a customized scheme.
anotherWebView.AddUrlScheme("myscheme");
anotherWebView.OnMessageReceived += (view, message) => {
    print(message.Scheme);
};
anotherWebView.Load("myscheme://action");

// => "myscheme"

The path of this UniWebViewMessage.

NOTICE

This will be the decoded value for the path of original url.

Example

webView.OnMessageReceived += (view, message) => {
    print(message.Scheme);
};
webView.Load("uniwebview://action?key=value&anotherKey=value");
// => "action"

// Encoded path
webView.OnMessageReceived += (view, message) => {
    print(message.Scheme);
};
webView.Load("uniwebview://%e8%b7%af%e5%be%84?key=value&anotherKey=value");
// => "路径"

The arguments of this UniWebViewMessage. UniWebView will try to parse the url query into a dictionary.

When received url "uniwebview://yourPath?param1=value1&param2=value2", the args is a Dictionary with: Args["param1"] = value1, Args["param2"] = value2

NOTICE

Both the key and value will be url decoded from the original url.

Example

// Basic key-value args
webView.OnMessageReceived += (view, message) => {
    print(message.Args["key"]);
    print(message.Args["anotherKey"]);
};
webView.Load("uniwebview://action?key=value&anotherKey=anotherValue");
// => "value"
// => "anotherValue"

// With the same key
webView.OnMessageReceived += (view, message) => {
    print(message.Args["key"]);
};
var message = new UniWebViewMessage("uniwebview://sample_message?key=1&key=2");
// => "1,2"

// With encoded key and value
webView.OnMessageReceived += (view, message) => {
    print(message.Args["键"]);
};
var message = new UniWebViewMessage("uniwebview://sample_message?%E9%94%AE=%E5%80%BC);
// => "值"

Methods

Initializes a new instance of the UniWebViewMessage struct.

Parameters
  • stringrawMessage

    Raw message which will be parsed to a UniWebViewMessage.