简体   繁体   中英

How to open 'window.open' in wkwebview

I developing hybrid app using cordova . . . I write below code in WkWebvieUIDelegate.m I can open a tag (target _blank with new window) and i can see NSLog this code

But i can't open window.open(...) in javascript and i can't see NSLog

how can i open window.open

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    webView.configuration.preferences.javaScriptEnabled = YES;


  webView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;


    NSLog(@"1");
    NSURL* url = [navigationAction.request URL];
    UIApplication *app = [UIApplication sharedApplication];


    if ([app canOpenURL:url]) {
         NSLog(@"2");
        [app openURL:url];
    } else {
//        if (!navigationAction.targetFrame.isMainFrame) {
//
//            [webView loadRequest:navigationAction.request];
//        }
    }
    return nil;
}

Firstly create an extension on WKWebView that defines a 'runJavaScriptInMainFrame:' method. In the extension method, use NSInvocationOperation

The NSInvocationOperation class is a concrete subclass of NSOperation that you use to initiate an operation that consists of invoking a selector on a specified object. This class implements a non-concurrent operation.

to call the undocumented '_runJavaScriptInMainFrame:' method.

 extension WKWebView {
        func runJavaScriptInMainFrame(#scriptString: NSString) -> Void {
            let selector : Selector = "_runJavaScriptInMainFrame:"
            let invocation = NSInvocationOperation(target: self, selector: selector, object: scriptString)
            NSOperationQueue.mainQueue().addOperation(invocation)
        }
    }

Now call this one:-

webview.runJavacriptInMainFrame:(scriptString: "Write Particular javascript code")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM