0

I am using webview for parsing my HTML data. For this, I am using a custom render.

In the Main Project:

public  class MyWebView : WebView
{
    public static readonly BindableProperty UrlProperty = BindableProperty.Create(
    propertyName: "Url",
    returnType: typeof(string),
    declaringType: typeof(MyWebView),
    defaultValue: default(string));

    public string Url
    {
        get { return (string)GetValue(UrlProperty); }
        set { SetValue(UrlProperty, value); }
    }
}

Ios Renderer

public class MyWebViewRenderer : ViewRenderer<MyWebView, WKWebView>
{
    WKWebView _wkWebView;
    protected override void OnElementChanged(ElementChangedEventArgs<MyWebView> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
        {
            var config = new WKWebViewConfiguration();
            _wkWebView = new WKWebView(Frame, config);
            SetNativeControl(_wkWebView);
        }
        if (e.NewElement != null)
        {
            Control.LoadHtmlString(Element.Url, null);   //use this code instead
            //Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Url)));
        }
    }
}

In XAML and XAML.cs:

<local:MyWebView 
    x:Name="web_view"
    VerticalOptions="FillAndExpand"/>

web_view.Url = htmldata;

But I am getting System.ArgumentNullException when running the project.

Screenshots:

enter image description here

Sreejith Sree
  • 3,055
  • 4
  • 36
  • 105

3 Answers3

1

The LoadHtmlString method I believe is what's causing the exception, Now it says that its an ArgumentNullException i.e. An Argument that should not be null is null and to fix that you should not pass null there for WkWebView you can simply pass the NSBundle's MainBundle BaseUrl as shown below:

  Control.LoadHtmlString(new NSString(Element?.Url), NSBundle.MainBundle.BundleUrl);

If the issue persists please make sure that you add the StackTrace in the question.

Goodluck, Feel free to get back if you have questions.

UPDATE

Also, you can use WkWebViewRenderer if you have XF 4.4 and above!

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
1

WebViewRenderer always displaying Hello world (quite small if no styling applied):

//'WebViewRenderer' is obsolete: 'WebViewRenderer is obsolete as of 4.4.0. Please use the WkWebViewRenderer instead'
//WkWebViewRenderer inherits from WKWebView
public class MyWebViewRenderer : WkWebViewRenderer
{
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            var myWebView = (MyWebView)e.NewElement;
            if (myWebView != null) 
            {
                ...
            }
            WKWebView wkWebView = this;
            var htmlString = "<html><body>Hello world</body></html>";
            wkWebView.LoadHtmlString(htmlString, null);
            ...
Benl
  • 2,777
  • 9
  • 13
0

Got the answer from my XF thread, posting it here. A sample project is also uploaded there.

I found you consumed a .Net Framework library in Forms project, please remove it and use .Net Standard HttpClient. Secondly, try to disable the ATS for your certain URL like:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>services.catholicbrain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
    </dict>
</dict>

Finally, move the loading URL code to OnElementPropertyChanged like what @Graverobber said:

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged(sender, e);

    if (e.PropertyName == "Url")
    {
        Control.LoadHtmlString(Element.Url, null);
    }
}
Sreejith Sree
  • 3,055
  • 4
  • 36
  • 105