首页 文章

Window.open()在WKWebView中不起作用

提问于
浏览 895
2

我试图在应用程序的iOS WKWebView中单击它时在safari浏览器中打开一个URL,但是当我从javascript调用window.open()时没有任何效果 .

它没有达到WKWebView的决定政策,

[Foundation.Export("webViewecidePolicyForNavigationActionecisionHandler:")]
public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)

我试图覆盖WKUIDelegate方法,但它不起作用 . :(

任何人都可以帮我吗?

2 回答

  • 2

    注意 - 我没有xamarin经验,但我知道“window.open()”js如何在WKWebView中使用本机代码,所以我希望这个答案可以帮助你连接丢失的点 .

    首先,在您配置Web视图的位置设置UIDelegate ..(在您的案例中为viewDidLoad)

    webView.uiDelegate = self
    

    其次,实现UIDelegate的这种协议方法 . 我们的想法是即时创建一个Web视图和一个视图控制器,然后呈现/推送它并将此Web视图实例返回给WebKit,以便它可以与父Web视图 Build 链接并加载url(窗口的第一个参数) . 公开电话) .

    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
            let childWebView = WKWebView(frame: .zero, configuration: configuration)   // Must use the configuration provided by this method
            let webViewController = ViewController() // create an instance of a new view controller that you want to push or present with a web view
            webViewController.webView = childWebView // provide this new child web view to view controller for layout purpose 
            navigationController?.pushViewController(webViewController, animated: true)
            return childWebView
    }
    
  • 0
    • 由于方法 DecidePolicy 包含在 IWKNavigationDelegate 中,因此您应该实现它 WKUIDelegate .

    • Export 内的方法字符串不完整,应为 [Export("webView:decidePolicyForNavigationAction:decisionHandler:")]

    有两种方法可以实现委托 .

    Strong Delegate:

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        WKWebView webview = new WKWebView(this.View.Bounds,null);
        webview.NavigationDelegate = new MyWKNavigationDelegate();
    }
    
    class MyWKNavigationDelegate : WKNavigationDelegate
    {
        public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
        {           
        }
    }
    

    Weak Delegate:

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        WKWebView webview = new WKWebView(this.View.Bounds,null);
        webview.WeakNavigationDelegate = this;
    }
    
    [Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
    public  void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction,  Action<WKNavigationActionPolicy> decisionHandler)
    {
    }
    

相关问题