Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does createWebviewWithConfiguration not change the displayed view when window.open is called?

I'm quite new to WKWebView and am struggling with this problem where my current view doesnt change when invoking windows.open from javascript see below...

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

I'm quite sure that this function is hit since I stepped on it using Safari Tech Preview. And I'm even getting the breakpoint in my createWebviewWithConfiguration() function in xcode obj-c when logo_click is invoked,

-(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;
}

I even tried hardcoding google.com to see if it can navigate from there but my displayed view is still the previous one. I already checked the tutorial on

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

but my view still doesn't change. What could I be missing?

like image 354
debonaire Avatar asked Nov 09 '22 08:11

debonaire


1 Answers

I solved it using the following code...

-(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;
}
like image 61
debonaire Avatar answered Nov 15 '22 05:11

debonaire