//isA && window.h5CallNative.getName(hrefT);//安卓调用方式
//isI && window.webkit.messageHandlers.getName(hrefT);//iOS调用方式
h5那边给了这种调用iOS原生函数的方式,本文介绍messageHandlers方式实现js调用原生函数做页面跳转
话不多说,步骤如下:
1.加入头文件
#import <WebKit/WebKit.h>
2.引入代理
WKNavigationDelegate,WKUIDelegate
3.申明必要的控件
@property(nonatomic, strong) WKWebView *webView;
/** 进度条 */
@property(nonatomic, strong) UIProgressView *progressView;
/** 请求 */
@property(nonatomic, strong) NSMutableURLRequest *request;
4.控件实现
- (void)creatWebView {
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.userContentController = [[WKUserContentController alloc] init];
// 交互对象设置
[config.userContentController addScriptMessageHandler:self name:@"getName"];
[config.userContentController addScriptMessageHandler:self name:@"showAlert"];
// 支持内嵌视频播放,不然网页中的视频无法播放
config.allowsInlineMediaPlayback = YES;
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-k_top_height) configuration:config];
self.webView.scrollView.delegate = self;
self.webView.navigationDelegate = self;
self.webView.UIDelegate = self;
// 开始右滑返回手势
self.webView.allowsBackForwardNavigationGestures = YES;
self.webView.scrollView.bounces = NO;
self.webView.backgroundColor = [UIColor whiteColor];
[self.webView loadRequest:self.request];
[self.mainView addSubview:self.webView];
// 添加观察者
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL]; // 进度
}

5.代理里面截获js方法
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message {
if([message.nameisEqualToString:@"getName"]) {
NSLog(@"currentThread ------ %@", [NSThread currentThread]);
//需要刷新页面的话需要去主线程
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary*jsDic =@{@"token": [LJDeviceUtilaccessToken],
@"deviceId": [LJDeviceUtiluuid],
@"versionCode":kMyVersion,
@"appType":@"qianrenzhang"
};
NSString*jsStr = [NSStringstringWithFormat:@"setToken('%@')", [LJCommonUtildictionaryToJson:jsDic]];
[self.webViewevaluateJavaScript:jsStrcompletionHandler:^(id_Nullableresult,NSError*_Nullableerror) {
NSLog(@"%@----%@",result, error);
}];
});
}elseif([message.nameisEqualToString:@"showAlert"]) {
NSDictionary *dic = [NSDictionary dictionaryWithDictionary:message.body];
NSLog(@"JS交互参数:%@", dic);
NSLog(@"JS交互参数:%@", [dicobjectForKey:@"code"]);
NSLog(@"JS交互参数:%@", [dic objectForKey:@"message"]);
//不刷新页面的
return;
}else{
return;
}
}

自此js调用oc方法成功
以下是加载进度条的方式
#pragma mark - 监听加载进度
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
if ([keyPath isEqualToString:@"estimatedProgress"]) {
if(object ==_webView) {
[self.progressViewsetAlpha:1.0f];
[self.progressView setProgress:self.webView.estimatedProgress animated:YES];
if(self.webView.estimatedProgress>=1.0f) {
[UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.progressViewsetAlpha:0.0f];
}completion:^(BOOLfinished) {
[self.progressViewsetProgress:0.0fanimated:NO];
}];
}
}else{
[superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:context];
}
}else{
[superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:context];
}
}

iOS给js传值
- (void)evaluateJS:(NSDictionary*)dic{
dispatch_async(dispatch_get_main_queue(), ^{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:nil];
NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *jsStr = [NSString stringWithFormat:@"addCommentsSuccess('%@')", jsonStr];
[self.webViewevaluateJavaScript:jsStrcompletionHandler:^(id_Nullableresult,NSError*_Nullableerror) {
NSLog(@"%@----%@",result, error);
}];
});
}
添加cookie

网友评论