Forum Xamarin.iOS


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

WKWebView control

OC_CarlOC_Carl USMember ✭✭

When I create a Mac Cocoa app there are 2 WebKit View controls available for use:

  • Legacy WebView with direction to use WKWebView
  • WKWebView the core view class in the Web Kit Framework

When "connecting" the legacy control to ViewControl.h, #import <WebKit/WebKit.h> needs to be added to the file.

However, when I connect a WKWebView control there is a red alert of "Unknown type name".

On iOS > User Interface > Controls > Web Views > Disambiguating iOS web view options page, I found this statement:
"WKWebView can also be used within Xamarin.Mac apps, and you therefore may want to consider using it if you are creating a cross-platform Mac/iOS app."

However, since there is no Cocoa template yet available for a Multiplatform Xamarin Forms app, one uses the work around to manually install the Xamarin Forms package into a Cocoa Mac App, but still no WKWebView. I notice that if one has both "using Xamarin.Forms;" AND "using WebKit;" the WebView type is ambiguous and WKWebView type could not be found.

Is there, where is, c# (Mono/Xamarin) support for the Xcode WKWebView control for a Mac app?

Best Answer


  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    So as you found, there are two web views APIs on macOS:

    The "original" full featured API:

    And a "cross platform" more limited API that works on iOS:

    both of them from Apple.

    We bound both of them in the WebKit namespace.

    I believe the "the WebView type is ambiguous and WKWebView type could not be found." issue you are running into is a namespace conflict between Xamarin.Forms and Xamarin.Mac, as they both have a WebView class.

    You can just fully specific it (WebKit.WebView) if you want the Xamarin.Mac version (or use the forms namespace fully specified).

  • OC_CarlOC_Carl USMember ✭✭

    Thank you for your response. So, the implication of your answer is neither Mac WebKit space currently contains a type definition for WKWebView for Mac.

    When there is a Multiplatform Xamarin Forms Mac app template in the future, will it contain the type definition for WKWebView as there is now for iOS? (To satisfy the currently published statement "WKWebView can also be used within Xamarin.Mac apps ...") Basically WKWebView cannot be used in a Mac app as of this writing, but will it be available sometime soon?

  • OC_CarlOC_Carl USMember ✭✭

    OK, great! Only with your confirmation was I able to get past the red alerts in Xcode to discover that the WKWebView control is in fact defined in ViewController.designer.cs and "using WebKit" added the type definition I needed.

    I got stumped when I encountered the "unknown type name" in Xcode not realizing the control would still be defined in the Mac app AND that I should use WebKit. (I think I wandered into Xamarin Forms because that is where WKWebView seemed to used for iOS, not sure, and thus made the leap into Xamarin Forms for Mac.)

    I m guessing there is not much of a use case for adding the WKWebView type definition to a #import for the ViewController.h file.

    Thanks again for pointing me in the right direction.

  • joseluisctjoseluisct ESMember ✭✭
    edited May 2018

    I'm having the same issue. I'm working on a Xamarin.Forms app with a WebView and in MacOS custom rendered. I think by default it is using WebKit.WebView.

    I couldn't find WebKit.WebView in Visual Studio for Mac, but now I see the class.

    I'm having issues loading a local web app (with file:///), but this app works OK in Safari browser. I think that maybe WebKit.WKWebView behaves as Safari does and it's what I need. ¿It's that true?

    ¿How could I change it to use WebKit.WKWebView in custom rendered? Thanks.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    @joseluisct You've commented on a thread from last year and with what at first glance is an unrelated issue. Consider creating a new thread.

Sign In or Register to comment.