iOS⽅法使⽤
#import "JsBridgeViewController.h"
#import "WKWebViewJavascriptBridge.h"
#import "WebViewJavascriptBridge.h"
@interface JsBridgeViewController ()<WKNavigationDelegate>
@property(nonatomic,strong) WKWebViewJavascriptBridge* bridge;
@property (nonatomic, strong)WKWebView * wkWebView;
@end
@implementation JsBridgeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.wkWebView];
[WKWebViewJavascriptBridge enableLogging];
self.bridge = [WKWebViewJavascriptBridge bridgeForWebView:self.wkWebView];
[self.bridge setWebViewDelegate:self];
//OC端的注册
//注册jscalloc⽅法,js调⽤oc,data为oc回调给js的参数
[self.bridge registerHandler:@"getBlogNameFromObjC" handler:^(id data, WVJBResponseCallback responseCallback) {        NSLog(@"js call getBlogNameFromObjC, data from js is %@", data);
if (responseCallback) {
// 反馈给JS
responseCallback(@{@"blogName": @"测试blog"});
}
}];
------------------------------------------------------------------
//OC端的调⽤
//调⽤js中注册的showapiinner⽅法,不传⼊数据,没有回调
//[_bridge callHandler:@"showapiinner"];
//调⽤js中注册的showapiinner⽅法,传⼊数据str,没有回调
//[_bridge callHandler:@"showapiinner" data:str];
原生js和js的区别//调⽤js中注册的showapiinner⽅法,传⼊数据str,且有回调(可以获取到回调参数responData)
// [_bridge callHandler:@"showapiinner" data:str responseCallback:^(id //responseData) {
// NSLog(@"%@",responseData);
// }];
[self renderButtons:self.wkWebView];
[self loadExamplePage:self.wkWebView];
}
//native调js  (native按钮)
- (void)callHandler:(id)sender {
[self.bridge callHandler:@"getUserInfos" data:@{@"name": @"测试"} responseCallback:^(id responseData) {
NSLog(@"from js: %@", responseData);
}];
}
- (void)renderButtons:(WKWebView*)webView {
UIFont* font = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
UIButton *callbackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[callbackButton setTitle:@"Call handler" forState:UIControlStateNormal];
[callbackButton addTarget:self action:@selector(callHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:callbackButton aboveSubview:webView];
callbackButton.frame = CGRectMake(10, 400, 100, 35);
callbackButton.titleLabel.font = font;
UIButton* reloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[reloadButton setTitle:@"Reload webview" forState:UIControlStateNormal];
[reloadButton addTarget:webView action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:reloadButton aboveSubview:webView];
reloadButton.frame = CGRectMake(110, 400, 100, 35);
reloadButton.titleLabel.font = font;
}
- (void)loadExamplePage:(WKWebView*)webView {
NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"demo0" ofType:@"html"];

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。