How to implement WKWebview in xamarin.forms ?

Hello,

I want to pass one url in my webview. But as IOS appstore gives error so I want to implement WKWebview but I want to know how can implement it? Please help me out its urgent.

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭

    Its not an error, its a warning. And there are many existing threads on the subject.

  • JarvanJarvan Member, Xamarin Team Xamurai

    You can use WKWebView via Custom Renderer. Create a custom WebView in your PCL and define a bindable property pointing what url should be loaded on iOS.

    public class local : CustomWebView: WebView
    {
        public static readonly BindableProperty UrlProperty = BindableProperty.Create(
            propertyName: "Url",
            returnType: typeof(string),
            declaringType: typeof(CustomWebView),
            defaultValue: default(string));
    
        public string Url
        {
            get { return (string)GetValue(UrlProperty); }
            set { SetValue(UrlProperty, value); }
        }
    }
    

    CustomRenderer.cs

    [assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]
    namespace WKWebViewDemo.iOS
    {
        public class CustomWebViewRenderer : ViewRenderer<CustomWebView, WKWebView>
        {
            WKWebView wkWebView;
            protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
            {
                base.OnElementChanged(e);
    
                if (Control == null)
                {
                    var config = new WKWebViewConfiguration();
                    wkWebView = new WKWebView(Frame, config);
                    SetNativeControl(wkWebView);
                }
    
                if (e.NewElement != null)
                {
                    Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Url)));
                }
            }
        }
    }
    

    Then se the custom view in page.xaml

    <StackLayout>
        <local:CustomWebView Url="https://www.google.com" VerticalOptions="FillAndExpand"/>
    </StackLayout>
    

    Refer to:
    https://stackoverflow.com/questions/42504016/how-can-i-use-wkwebview-instead-of-uiwebview-in-xamarin-forms-webviewios/42524772

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited November 2019

    @Gandhi_123 Any updates? If you have solved the problem, please mark the helpful solution as answer which will help others who face the similar problem. If you are facing some issues while implementing, try to post the particular error with the corresponding codes here.

Sign In or Register to comment.