Search code examples
iosxamarinipadxamarin.ioswkwebview

Xamarin.Forms WebView, WKWebViewConfiguration, and Sending "Mobile" Headers


I'm getting back into Xamarin.Forms, and I've discovered a new behavior of iOS 13 Safari on the iPad called "Desktop-Class Browsing." It's covered in detail here:

https://developer.apple.com/videos/play/wwdc2019/203

The problem for me is that I need my Web application to be able to detect if a normal Web browser is viewing it or if it's being viewed by a Xamarin.Forms.WebView. From my Web application, I used to be able to detect a Web view with the user-agent string or with the X-Requested-With header. But if my Web view is running on an iPad on iOS 13, it will enter desktop mode, which yields a desktop Safari user agent a no X-Requested-With header, and I can no longer detect that it is a Web view. This is referenced at 2:56 in the above video.

In a native iOS application, I can apparently use WKWebViewConfiguration to control these desktop-like featuers:

https://developer.apple.com/documentation/webkit/wkwebviewconfiguration

I think this is demonstrated at 9:01 in the video in my first link.

How can I use WKWebViewConfiguration with Xamarin.Forms? I see a WebKit.WKWebViewConfiguration class, but how can I utilize it with Xamarin.Forms.WebView?


Solution

  • Unless you follow the steps in this article, your app is using UIWebView instead of a WKWebView

    There are several ways of getting access to editing the WKWebViewConfiguration:

    • You can use a "Custom Renderer" to implement your own WKWebView as shown in this Official Xamarin Forms example, and edit the WKWebViewConfiguration there.
    • Another possible way that I haven't tried- You can also implement a WKWebView inside your forms using the steps mentioned here. And then just edit that property.