给HTML添加JS代码
NSString *js = @"function xx() {return '我是事先注入的JS代码';}";
//初始化WKUserScript对象
//WKUserScriptInjectionTimeAtDocumentEnd为网页加载完成时注入
WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
//根据生成的WKUserScript对象,初始化WKWebViewConfiguration
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addUserScript:script];
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
[self.view addSubview:self.webView];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"htmlTest" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.UIDelegate = self;
self.webView.navigationDelegate = self;
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
NSString *jsFun = [NSString stringWithFormat:@"xx()"];
[self.webView evaluateJavaScript:jsFun completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
NSLog(@"%@---%@",error,obj);
}];
}
进度监听
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.webView];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"htmlTest" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:(NSKeyValueObservingOptionNew) context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@",change[@"new"]);
}
获取HTML标签值
你好!
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.webView];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"htmlTest" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.UIDelegate = self;
self.webView.navigationDelegate = self;
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
NSString *jsFun = @"document.getElementById('word').innerHTML";
[self.webView evaluateJavaScript:jsFun completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
NSLog(@"%@---%@",error,obj);
}];
}
左滑返回
OC调用JS方法传参/返回值
传递单个字符串:
注意要加单引号,返回值在block回调里面
NSString *jsFun = [NSString stringWithFormat:@"getJsCode('%@')",@"你好!”];
[self.webView evaluateJavaScript:jsFun completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
NSLog(@"%@---%@",error,obj);
}];
传递json
NSDictionary *dict = @{
@"name": @"张三",
@"age": @(18)
};
NSData *data = [NSJSONSerialization dataWithJSONObject:dict
options:(NSJSONWritingPrettyPrinted)
error:nil];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *jsFun = [NSString stringWithFormat:@"getJsCode(%@)",str];
[self.webView evaluateJavaScript:jsFun completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
NSLog(@"%@---%@",error,obj);
}];
你好!
JS调用OC方法传参/返回值
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
[self.view addSubview:self.webView];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"htmlTest" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.UIDelegate = self;
self.webView.navigationDelegate = self;
[config.userContentController addScriptMessageHandler:self name:@"jsToOC"];
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
if ([message.name isEqualToString:@"jsToOC"]) {
NSLog(@"---%@",message.body);
}
}
你好!