WKWebView与js交互

js调用oc方法

第一种

html代码







    
    
    

返回值:

ios代码

#import "ViewController.h"
#import 

@interface ViewController ()
@property(nonatomic, weak) WKWebView *webView;
@end

@implementation ViewController

- (void)dealloc{
    [self.webView.configuration.userContentController removeAllUserScripts];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // WKWebView的配置
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    //是否支持JavaScript
    configuration.preferences.javaScriptEnabled = true;
    //不通过用户交互,是否可以打开窗口
    configuration.preferences.javaScriptCanOpenWindowsAutomatically = NO;
    
    WKUserContentController *userContentController = [[WKUserContentController alloc] init];
    [userContentController addScriptMessageHandler:self name:@"jsCallNative1"];
    [userContentController addScriptMessageHandler:self name:@"jsCallNative2"];
    [userContentController addScriptMessageHandler:self name:@"jsCallNative3"];
    configuration.userContentController = userContentController;
    
    WKWebView *webView = [[WKWebView alloc]initWithFrame: self.view.bounds configuration: configuration];
    [self.view addSubview:webView];
    self.webView = webView;
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL* url = [NSURL fileURLWithPath:path];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

#pragma mark - WKScriptMessageHandler
- (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
    if ([message.name isEqualToString:@"jsCallNative1"]) {
        NSLog(@"调用成功");
    }
    if ([message.name isEqualToString:@"jsCallNative2"]) {
        NSArray *parameters = message.body;
        NSLog(@"参数 %@",parameters);
    }
    if ([message.name isEqualToString:@"jsCallNative3"]) {
        NSArray *parameters = message.body;
        NSLog(@"参数 %@",parameters);
    }
}

#pragma mark - WKUIDelegate delegate method
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable result))completionHandler {
    if ([prompt isEqualToString: @"buttonClick3"]) {
        completionHandler(@"将内容回调给js");
    }
}
@end

oc调用js方法

html代码







    
    

返回值:

oc代码

#import "ViewController.h"
#import 

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // WKWebView的配置
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    //是否支持JavaScript
    configuration.preferences.javaScriptEnabled = true;
    //不通过用户交互,是否可以打开窗口
    configuration.preferences.javaScriptCanOpenWindowsAutomatically = NO;
    WKWebView *webView = [[WKWebView alloc]initWithFrame: self.view.bounds configuration: configuration];
    [self.view addSubview:webView];
    webView.navigationDelegate = self;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL* url = [NSURL fileURLWithPath:path];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    [webView evaluateJavaScript:@"ocCallJs('Hello World')" completionHandler:^(id response, NSError * _Nullable error) {
        NSLog(@"response = %@", response);
    }];
}
@end

你可能感兴趣的:(WKWebView与js交互)