我试图在应用程序的iOS WKWebView中单击它时在Safari浏览器中打开一个URL,但是当我从没有效果的javascript调用window.open()时。
它没有达到WKWebView中的决定政策,
[Foundation.Export("webViewecidePolicyForNavigationActionecisionHandler:")]
public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
我试图覆盖WKUIDelegate方法,但它不起作用。 :(
有人可以帮帮我吗?
答案 0 :(得分:2)
注意 - 我没有xamarin经验,但我知道&#34; window.open()&#34; js在WKWebView中使用本机代码,所以我希望这个答案可以帮助你连接丢失的点。
首先,在您配置Web视图的地方设置UIDelegate ..(在您的案例中为viewDidLoad)
webView.uiDelegate = self
其次,实现UIDelegate的这种协议方法。我们的想法是即时创建一个Web视图和一个视图控制器,然后呈现/推送它并将此Web视图实例返回给WebKit,以便它可以与父Web视图建立链接并加载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
}
答案 1 :(得分:0)
由于方法DecidePolicy
包含在IWKNavigationDelegate
中,因此您应该将其实现为WKUIDelegate
。
Export
内的方法字符串不完整,应为[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
有两种方法可以实现委托。
强代表:
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)
{
}
}
弱代表:
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)
{
}