admin管理员组

文章数量:1410673

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 overrides webView content. Is it possible to run JavaScript code in webView page? Not in other window, only in page with page content.

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 overrides webView content. Is it possible to run JavaScript code in webView page? Not in other window, only in page with page content.

Share Improve this question edited Dec 22, 2023 at 8:02 stackich 5,3755 gold badges27 silver badges55 bronze badges asked Sep 15, 2015 at 9:15 user5305666user5305666 3
  • Are you using a UIWebView or WKWebView? I remend using WKWebView for using javascript methods AFTER the view has loaded. – Nishant Commented Sep 15, 2015 at 9:21
  • I'm using UIWebView,thanks for advice – user5305666 Commented Sep 15, 2015 at 9:25
  • you want to inject a .js file or you want to execute some javascript code? – jcesarmobile Commented Sep 15, 2015 at 9:46
Add a ment  | 

1 Answer 1

Reset to default 5

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()

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
             pletionHandler:^(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 = "existingMethodWithParam()"
self.wkWebView?.evaluateJavaScript(callMethod) { obj, error in
    print(obj)
    print(error)
}

本文标签: iosLoad local javascript in webviewStack Overflow