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

Answers

  • ChrisHamonsChrisHamons USXamarin Team Xamurai

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

    The "original" full featured API:

    https://github.com/xamarin/xamarin-macios/blob/master/src/webkit.cs

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

    https://github.com/xamarin/xamarin-macios/blob/master/src/wkwebkit.cs

    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.

Sign In or Register to comment.