locked
Setting custom user agent on a WebView RRS feed

  • Question

  • User369433 posted

    Hello, Does anyone knows how to set custom user agent for a WebView both on Android and iOS. Unfortunately, I have not found a working solution.

    I will be very glad for any help.

    Monday, November 25, 2019 4:04 PM

All replies

  • User371688 posted

    If you need that functionality, you will need to use a DependencyService or CustomRenderer as outlined in these guides:

    Custom renderer: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/ DependencyService: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/

    For more details, you can check the following link:

    https://forums.xamarin.com/discussion/18327/how-do-i-set-the-user-agent-and-or-cookies-on-a-webview-in-a-webviewpage

    Tuesday, November 26, 2019 10:14 AM
  • User369433 posted

    @jezh I have checked this link earlier. Unfortunately, it does not work for me and also there is no solution for Android.

    Tuesday, November 26, 2019 2:58 PM
  • User371688 posted

    In order to help you better, could you please post a basic demo or code snippet you tried so that we can help you better?

    Thursday, November 28, 2019 2:38 AM
  • User369433 posted

    @jezh

    Ok. So let's take iOS for example. Setting a custom user should not be hard because WKWebView has CustomUserAgent property. https://docs.microsoft.com/en-us/dotnet/api/webkit.wkwebview.customuseragent?view=xamarin-ios-sdk-12

    Here is my example code for a renderer on iOS:

    ``` [assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))] namespace MobileTogether.iOS.Renderers { public class CustomWebViewRenderer : ViewRenderer { protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e);

            if (e.NewElement != null && Control != null)
            {
                Control.CustomUserAgent = "core_together_mobile_ios";
            }
        }
    }
    

    } ```

    However this is not working because now "Navigating" and "Navigated" events are not triggered.

    Thursday, November 28, 2019 3:01 PM