共用方式為


interface ICoreWebView2Settings2

Note

This reference is no longer being maintained. For the latest API reference, see WebView2 API Reference.

interface ICoreWebView2Settings2
  : public ICoreWebView2Settings

A continuation of the ICoreWebView2Settings interface that manages the user agent.

Summary

Members Descriptions
get_UserAgent Returns the User Agent.
put_UserAgent Sets the UserAgent property.

Applies to

Product Introduced
WebView2 Win32 1.0.864.35
WebView2 Win32 Prerelease 1.0.824

Members

get_UserAgent

Returns the User Agent.

public HRESULT get_UserAgent(LPWSTR * userAgent)

The default value is the default User Agent of the Microsoft Edge browser.

The caller must free the returned string with CoTaskMemFree. See API Conventions.

                if (m_settings2)
                {
                    static const PCWSTR url_compare_example = L"fourthcoffee.com";
                    wil::unique_bstr domain = GetDomainOfUri(uri.get());
                    const wchar_t* domains = domain.get();

                    if (wcscmp(url_compare_example, domains) == 0)
                    {
                        SetUserAgent(L"example_navigation_ua");
                    }
                }

put_UserAgent

Sets the UserAgent property.

public HRESULT put_UserAgent(LPCWSTR userAgent)

This property may be overridden if the User-Agent header is set in a request. If the parameter is empty the User Agent will not be updated and the current User Agent will remain. Setting this property may clear User Agent Client Hints headers Sec-CH-UA-* and script values from navigator.userAgentData. Current implementation behavior is subject to change. The User Agent set will also be effective on service workers and shared workers associated with the WebView. If there are multiple WebViews associated with the same service worker or shared worker, the last User Agent set will be used. Returns HRESULT_FROM_WIN32(ERROR_INVALID_STATE) if the owning WebView is closed.