- // 创建一个 WKwebView
- self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];
- NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil];
- NSURL *fileURL = [NSURL fileURLWithPath:urlStr];
- [self.webView loadFileURL:fileURL allowingReadAccessToURL:fileURL];
- self.webView.navigationDelegate = self;
- self.webView.UIDelegate = self;
- [self.view addSubview:self.webView];
JS 调用 OC 的方法
- // 配置环境
- WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc]init];
- userContentController =[[WKUserContentController alloc]init];
- configuration.userContentController = userContentController;
- // 注册方法
- [userContentController addScriptMessageHandler:self name:@"sayhello"];// 注册一个 name 为 sayhello 的 JS 方法
- // 移除
- -(void)viewWillDisappear:(BOOL)animated{
- [super viewWillDisappear:animated];
- // 这里需要注意, 前面增加过的方法一定要 remove 掉, 不能在 dealloc 里面做这件事,
- // 因为有 "sayhello" 这个时间的存在所以 dealloc 这个方法并不会走. 不信的你可以试一试.
- // 在这里我没有跳转二级界面用了这中懒人做法, 其实正确的做法是写一个代理方法的类
- // 专一注册才可以
- [userContentController removeScriptMessageHandlerForName:@"sayhello"];
- }
- #pragma mark - WKScriptMessageHandler
- - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
- NSLog(@"name:%@\\\\n body:%@\\\\n frameInfo:%@\\\\n",message.name,message.body,message.frameInfo);
- }
JS 调用 OC 注册的最正统的方法
- @interface ViewController ()<WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler>{
- WKWebView * webView;
- WKUserContentController* userContentController;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // 配置环境
- WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc]init];
- userContentController =[[WKUserContentController alloc]init];
- configuration.userContentController = userContentController;
- webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, 100, 100) configuration:configuration];
- // 注册方法
- WKDelegateController * delegateController = [[WKDelegateController alloc]init];
- delegateController.delegate = self;
- [userContentController addScriptMessageHandler:delegateController name:@"sayhello"];
- }
- - (void)dealloc{
- // 这里需要注意, 前面增加过的方法一定要 remove 掉.
- [userContentController removeScriptMessageHandlerForName:@"sayhello"];
- }
WKDelegateController 代码:
- #import <UIKit/UIKit.h>
- #import <WebKit/WebKit.h>
- @protocol WKDelegate <NSObject>
- - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message;
- @end
- @interface WKDelegateController : UIViewController <WKScriptMessageHandler>
- @property (weak , nonatomic) id<WKDelegate> delegate;
- @end
- #import "WKDelegateController.h"
- @interface WKDelegateController ()
- @end
- @implementation WKDelegateController
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
- if ([self.delegate respondsToSelector:@selector(userContentController:didReceiveScriptMessage:)]) {
- [self.delegate userContentController:userContentController didReceiveScriptMessage:message];
- }
- }
- @end
oc 调用 JS 方法
- //say() 是 JS 方法名, completionHandler 是异步回调 block
- [webView evaluateJavaScript:@"say()" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
- NSLog(@"%@",result);
- }];
来源: http://www.jianshu.com/p/3526f599e075