简体   繁体   中英

Load local javascript in webview?

I'm making mobile browser with my own ads in swift and i want to run script.I already inject javascript local file but it opens in other window and override webview content.Is it possible to run javascript code in webview page? not in other window,only in page with page content.Hope you'll get it

Using WKWebView for javascript method call:

Step 1. Import class for webkit

Obj-C: #import <WebKit/WebKit.h>
Swift: import WebKit

Step 2. Create a WKWebView instance (Unfortunately it can only be done programatically)

Obj-C @property (strong, nonatomic) WKWebView *wkWebView;

Swift var wkWebView: WKWebView? = WKWebView() var wkWebView: WKWebView? = WKWebView()

Step 3. Then in viewDidLoad

Obj-C:

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];

    NSString *scriptSourceCode = @"your javascript code here";
    WKUserScript *script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];

    WKUserContentController *controller = [[WKUserContentController alloc] init];
    [controller addUserScript:script];

    configuration.userContentController = controller;

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

    [self.view addSubview:_wkWebView];

Swift:

    let configuration = WKWebViewConfiguration()
    let controller = WKUserContentController()

    let scriptSourceCode = "your javascript code here"
    let script = WKUserScript(source: scriptSourceCode, injectionTime: WKUserScriptInjectionTime.AtDocumentStart, forMainFrameOnly: true)
    controller.addUserScript(script)

    configuration.userContentController = controller

    self.wkWebView = WKWebView(frame: self.view.frame, configuration: configuration)

    self.view.addSubview(self.wkWebView!)

Step 4 Load a HTML string (or a loadRequest) in the wkWebView :

Obj-C:

[_wkWebView loadHTMLString:_desiredHTML baseURL:[[NSBundle mainBundle] bundleURL]];

Swift:

self.wkWebView?.loadHTMLString(_desiredHTML, baseURL: NSBundle.mainBundle().bundleURL)

Step 5 . Call a method which exists in the javascript included in the WKWebView

Obj-C:

    NSString *callMethod = [NSString stringWithFormat:@"existingMethodWithParam(%@)", stringParam];

    [_wkWebView evaluateJavaScript:callMethod
             completionHandler:^(id obj, NSError *error) {

                 if (error)
                 {
                     NSLog(@"ERROR evaluating JavaScript - %@", error);
                 }
                 else 
                 {
                     NSLog(@"Returned value - %@", obj); //if the javascript method has a return type
                 }
             }];

Swift:

    let callMethod = String(format: "existingMethodWithParam(%@)", stringParam)

    [self.wkWebView?.evaluateJavaScript(callMethod, completionHandler: { (obj, error) -> Void in

        println(obj)
        println(error)
    })]

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