如何查找Url是否已完成在Xamarin上的IOS中的自定义HybridWebview内加载

问题描述 投票:0回答:1

我正在开发xamarin表单HCHybridWebview,我的目标是在网址完全加载到IOS后捕获事件。我的代码如下。

这部分是根据文档制作的

  [assembly: ExportRenderer (typeof(HCHybridWebview), typeof(HCHybridWebviewRendererIos))]
namespace HashChing.iOS.CustomRenderers
   {

    public class HCHybridWebviewRendererIos : ViewRenderer<HCHybridWebview, WKWebView>, IWKScriptMessageHandler
    {
    public StorageService storageService => DependencyService.Get<StorageService>() ?? new StorageService();

    const string JavaScriptFunction = "function invokeCSharpAction(data){window.webkit.messageHandlers.invokeAction.postMessage(data);}";
    WKUserContentController userController;

    protected override void OnElementChanged (ElementChangedEventArgs<HCHybridWebview> e)
    {
        base.OnElementChanged (e); 

        if (Control == null) {
            userController = new WKUserContentController ();
            var script = new WKUserScript (new NSString (JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false);
            userController.AddUserScript (script);
            userController.AddScriptMessageHandler (this, "invokeAction");

            var config = new WKWebViewConfiguration { UserContentController = userController };
            var webView = new WKWebView (Frame, config);
            SetNativeControl (webView);
        }
        if (e.OldElement != null) {
            userController.RemoveAllUserScripts ();
            userController.RemoveScriptMessageHandler ("invokeAction");
            var hybridWebView = e.OldElement as HCHybridWebview;
            hybridWebView.Cleanup ();
        }
        if (e.NewElement != null) {
            var hybridView = e.NewElement as HCHybridWebview;
            if(hybridView != null)
            {
                hybridView.RefreshView += LoadUrl;
            }
            //string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", Element.Uri));

        }
     }

在同一个类中,这是我加载URL的地方

    public void LoadUrl(object sender, EventArgs e)
    {
        var webview = (HCHybridWebview)sender;
        var req = new NSMutableUrlRequest(new NSUrl(webview.weblink));

        NSMutableDictionary dic = new NSMutableDictionary();
        dic.Add(new NSString("Authorization"), new NSString("Bearer " + storageService.GetData<Token>("Token").token));
        req.Headers = dic;
        Control.LoadRequest(req);

    }

    public void DidReceiveScriptMessage (WKUserContentController userContentController, WKScriptMessage message)
    {
        Element.InvokeAction (message.Body.ToString ());
    }
  }
}

任何帮助都会非常感激

xamarin.forms xamarin.ios
1个回答
1
投票

解:

您可以在方法DidFinishNavigation中实现它

public class HCHybridWebviewRendererIos : ViewRenderer<HCHybridWebview, WKWebView>, IWKScriptMessageHandler,IWKNavigationDelegate

protected override void OnElementChanged (ElementChangedEventArgs<HCHybridWebview> e)
{
    base.OnElementChanged (e); 

    if (Control == null) {
        userController = new WKUserContentController ();
        var script = new WKUserScript (new NSString (JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false);
        userController.AddUserScript (script);
        userController.AddScriptMessageHandler (this, "invokeAction");

        var config = new WKWebViewConfiguration { UserContentController = userController };
        var webView = new WKWebView (Frame, config);

        webview.WeakNavigationDelegate=this;

        SetNativeControl (webView);
    }
}


[Export("webView:didFinishNavigation:")]
public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
  if(!webView.IsLoading)
  {
     // do some thing you want
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.