我正在尝试发送自定义字符串以及 WKWebView 中的现有用户代理,- Xamarin iOS

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

我正在尝试将自定义字符串与 WKWebView 中的现有用户代理一起发送,(Xamarin iOS)

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

我将此视为用户代理:

User Agent = System.Threading.Tasks.Task1[Foundation.NSObject]

User Agent + Custom = System.Threading.Tasks.Task1[Foundation.NSObject] + MyApp

但预计如下所示:

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

任何与此相关的帮助都会有所帮助。

谢谢,

使用下面的代码:

 public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.

            WKWebView WKWebView_New = new WKWebView(View.Frame, new WKWebViewConfiguration());

            WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => {
                if (err != null)
                {
                    System.Console.WriteLine(err);
                }
                if (result != null)
                {
                    System.Console.WriteLine(result);
                    var userAgent = result;
                    WKWebView_New.CustomUserAgent = userAgent + " + " + "MyApp";

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

            WKWebView_New.EvaluateJavaScript("navigator.userAgent", handler);

        }

然后就可以得到你想要的信息了:

User Agent + Custom = Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A366 + MyApp