Compartir a través de


interface ICoreWebView2Settings2

interface ICoreWebView2Settings2
  : public ICoreWebView2Settings

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

Summary

Members Descriptions
get_UserAgent Gets the UserAgent property.
put_UserAgent This setting determines the UserAgent of WebView.

Applies to

Product Introduced
WebView2 Win32 1.0.864.35
WebView2 Win32 Prerelease 1.0.824

Members

get_UserAgent

Gets the UserAgent property.

public HRESULT get_UserAgent(LPWSTR * value)

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

put_UserAgent

This setting determines the UserAgent of WebView.

public HRESULT put_UserAgent(LPCWSTR value)

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.

                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");
                    }
                }