Forum Xamarin Xamarin.iOS

WKWebView - UserAgent

I am trying to send a custom string along with the existing user agent in WKWebView,

        WKWebView WKWebView_New = new WKWebView(View.Frame, new WKWebViewConfiguration());
        var userAgent = WKWebView_New.EvaluateJavaScriptAsync("navigator.userAgent");
        WKWebView_New.CustomUserAgent =  userAgent + " + " + "MyApp";

        Console.WriteLine("User Agent = " + userAgent);
        Console.WriteLine("User Agent + Custom = " + WKWebView_New.CustomUserAgent);

I am seeing this as the user agent:

User Agent = System.Threading.Tasks.Task`1[Foundation.NSObject]

User Agent + Custom = System.Threading.Tasks.Task`1[Foundation.NSObject] + MyApp

But Expected to see as below:

User Agent = Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15G77

User Agent + Custom = Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15G77 + MyApp

Any help regd the same will be helpful.

Thank you,

Best Answers

  • AmruthaVarsha.TamanamAmruthaVarsha.Tamanam USMember ✭✭
    Accepted Answer

    @LandLu I have 2 WebViews in my project one with WKWebView (Called WKWebView_New) & UIWebView (Called webViewMain) for which I have to send different custom strings along with the UserAgent (For WKWebView MyApp & for UIWebView : app = iOS)

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Please notice that you've tried to use an Async method, this kind of method need an await prefix to make it work.
    Modify it like:

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
    
        addCustomAgent();
    }
    
    async void addCustomAgent()
    {
        WKWebView WKWebView_New = new WKWebView(View.Frame, new WKWebViewConfiguration());
        var userAgent = await WKWebView_New.EvaluateJavaScriptAsync("navigator.userAgent");
        WKWebView_New.CustomUserAgent = userAgent + " + " + "MyApp";
    
        Console.WriteLine("User Agent = " + userAgent);
        Console.WriteLine("User Agent + Custom = " + WKWebView_New.CustomUserAgent);
    }
    

    Or you can try another way to achieve that:

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
    
        WKWebView WKWebView_New = new WKWebView(View.Frame, new WKWebViewConfiguration());
        WKWebView_New.EvaluateJavaScript("navigator.userAgent", (result, error) =>
        {
            WKWebView_New.CustomUserAgent = result + " + " + "MyApp";
    
            Console.WriteLine("User Agent = " + result);
            Console.WriteLine("User Agent + Custom = " + WKWebView_New.CustomUserAgent);
        });          
    }
    
  • @LandLu Can you verify and please correct this code ?

    var userAgent = webViewMain.EvaluateJavascript("navigator.userAgent") + " ; " + "app = iOS";
    userAgent += " ; " + "app = iOS";
    Console.WriteLine("User Agent = " + userAgent);

    **Am seeing this :
    **
    First Click on Webpage:
    User Agent = Mozilla/5.0 (iPhone; CPU iPhone OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15F79 ; app = iOS

    Second Click on Webpage:
    User Agent = Mozilla/5.0 (iPhone; CPU iPhone OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15F79 ; app = iOS ; app = iOS

    **Expected:
    **First Click on Webpage:
    User Agent = Mozilla/5.0 (iPhone; CPU iPhone OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15F79 ; app = iOS

    Second Click on Webpage:
    User Agent = Mozilla/5.0 (iPhone; CPU iPhone OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15F79 ; app = iOS

    Every time I visit a webpage in my App app = iOS keeps added in the user agent, but I am looking for only once how many ever times i open webpage.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Why do you set your webView's CustomUserAgent over and over again? If you only wanted to set once, try not to use WKWebView_New.CustomUserAgent = result + " + " + "MyApp"; when you click the webpage.
    Or try to detect the CustomUserAgent, when you found it had involved the app = iOS, avoided adding it again.

    var userAgent = webViewMain.EvaluateJavascript("navigator.userAgent");
    if (!userAgent.ToString().Contains("app = iOS"))
    {
    
    }
    
  • AmruthaVarsha.TamanamAmruthaVarsha.Tamanam USMember ✭✭
    Accepted Answer

    @LandLu I have 2 WebViews in my project one with WKWebView (Called WKWebView_New) & UIWebView (Called webViewMain) for which I have to send different custom strings along with the UserAgent (For WKWebView MyApp & for UIWebView : app = iOS)

  • LandLuLandLu Member, Xamarin Team Xamurai

    How did you add user agent in UIWebView? If you use NSUserDefaults:

    UIWebView webView = new UIWebView();
    var userAgent = webView.EvaluateJavascript("navigator.userAgent") + " ; " + "app = iOS";
    NSDictionary dic = NSDictionary.FromObjectAndKey(new NSString(userAgent), new NSString("UserAgent"));
    NSUserDefaults.StandardUserDefaults.RegisterDefaults(dic);
    

    This will change the value the next time you want to use webView.EvaluateJavascript("navigator.userAgent") to get the user agent string. This code block shouldn't be used over and over again, you should just set it only one time. Then even though you use WKWebView to request a user agent, its value will be what you have set above.
    So why not use two WKWebViews with different custom user agent to load your web pages? User agent can be easily used as an exposed api in WKWebView.
    Moreover if you do want to use UIWebView, try to create a static tool class to set its user agent only once.

Sign In or Register to comment.