【问题标题】:Xamarin Forms: Custom webview is not working in IOSXamarin Forms:自定义 webview 在 IOS 中不起作用
【发布时间】:2020-05-08 07:55:10
【问题描述】:

我正在使用自定义 Webview 来解决 ITMS-90809: Deprecated API Usage - Apple will stop accepting submissions of apps that use UIWebView APIs. 和解析 HTML data

我的代码:

MyWebView.cs

using Xamarin.Forms;

namespace CustomWebview
{
    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

using CustomWebview;
using CustomWebview.iOS;
using WebKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]

namespace CustomWebview.iOS
{
    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)));
            }
        }
    }
}

当我打开具有 MyWebView 的页面时,页面重定向到 Main.cs 并显示以下异常。

我已经上传了一个示例项目here

【问题讨论】:

  • 参数Element.Url好像为null。确保您已在 xaml 中设置它。
  • @LucasZhang-MSFT 我已经硬编码了 HTML 字符串值。在这种情况下,自定义 webview 正在工作。在我的项目中,数据是从 get rest API 加载的。在获取数据之前我得到了异常。在到达以下行时,我的应用程序因此异常而损坏。 var response = await client.GetAsync("rest api");在设置 webview url 值之前,应用程序已损坏。
  • @LucasZhang-MSFT 你能检查一下这个sample project吗?我添加了一个私有 api,因为仅在调用 api 时出现问题。

标签: xamarin.forms webview argumentnullexception


【解决方案1】:

我发现在 Forms 项目中使用了一个 .Net Framework 库,请删除它并使用 .Net Standard HttpClient。 其次,尝试为您的特定 URL 禁用 ATS,例如:

<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>

最后,将加载 URL 代码移至 OnElementPropertyChanged。

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

    if (e.PropertyName == "Url")
    {
        Control.LoadHtmlString(Element.Url, null);
    }
}

这个答案来自我的另一个XF thread

【讨论】:

    猜你喜欢
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多