为什么在调用window.open时createWebviewWithConfiguration不会更改显示的视图?

时间:2016-09-19 09:00:20

标签: objective-c wkwebview window.open

我是WKWebView的新手,我正在努力解决这个问题,当我从javascript调用windows.open时我的当前视图没有变化,见下文...

function logo_click() {
    window.open(some_valid_url);//url is valid www.google.com for example
}

我很确定这个功能在我使用Safari Tech Preview踩到它之后就被击中了。当调用logo_click时,我甚至在xcode obj-c中的createWebviewWithConfiguration()函数中获取断点,

-(WKWebView*)webView:(WKWebView )webView createWebViewWithConfiguration:(nonnull WKWebViewConfiguration )inConfig forNavigationAction:(nonnull WKNavigationAction )navigationAction windowFeatures:(nonnull WKWindowFeatures )windowFeatures
{
 _webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:inConfig];

 if (!navigationAction.targetFrame.isMainFrame) {
  [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  [self.webView loadRequest:navigationAction.request];
  //[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com"]]];
 }

 return _webView;
}

我甚至尝试使用硬编码google.com查看它是否可以从那里导航,但我显示的视图仍然是前一个。我已经检查了

上的教程

iphonedevsdk.com/forum/iphone-sdk-development/122635-wkwebview-wont-open-external-links.html

但我的观点仍然没有改变。我能错过什么?

1 个答案:

答案 0 :(得分:0)

我使用以下代码解决了它...

-(WKWebView*)webView:(WKWebView *)webView createWebViewWithConfiguration:(nonnull WKWebViewConfiguration *)inConfig forNavigationAction:(nonnull WKNavigationAction *)navigationAction windowFeatures:(nonnull WKWindowFeatures *)windowFeatures
{
    [_webView removeFromSuperview];

    _webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:inConfig];

    if (!navigationAction.targetFrame.isMainFrame) {
        //[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        NSURLRequest* req = navigationAction.request;
        [self.webView loadRequest:req];
    }

    _webView.navigationDelegate = self;
    _webView.UIDelegate = self;

    _webView.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:_webView];

    return _webView;
}