代码里面 一共两种和js交互的方法注释的是另一种
点h文件
//
// ELE_testViewController.h
// ELE_Information
//
// Created by chengl on 16/6/7.
// Copyright © 2016年 eleme. All rights reserved.
//
#import "ELE_baseVIewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@protocol TestJSExport <JSExport>
-(void)close;
-(void)hideBar:(NSString *)str;
-(void)showBar:(NSString *)str;
@end
@interface ELE_testViewController : ELE_baseVIewController<TestJSExport>
@property (strong, nonatomic) JSContext *context;
@property (strong, nonatomic)UIWebView *webView;
@property (nonatomic,strong)NSString *urlStr;
@end
点m文件
//
// ELE_testViewController.m
// ELE_Information
//
// Created by chengl on 16/6/7.
// Copyright © 2016年 eleme. All rights reserved.
//
#import "ELE_testViewController.h"
@interface ELE_testViewController ()
@end
@implementation ELE_testViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *url=[[NSBundle mainBundle]URLForResource:@"obj" withExtension:@"html"];
NSMutableURLRequest *tmpRequest=[[NSMutableURLRequest alloc] initWithURL:url];
[self.webView loadRequest:tmpRequest];
}
-(UIWebView*)webView{
if (!_webView) {
_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,SCREEN_HEIGHT)];
_webView.delegate = (id)self;
_webView.scalesPageToFit = YES;
_webView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_webView];
}
return _webView;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.context =[self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
NSLog(@"异常信息:%@", exceptionValue);
};
self.context[@"App"] = self ;
// __weak typeof(self) weakSelf = self;
// self.context[@"showBar"] = ^() {
// __strong typeof(self) strongSelf = weakSelf;
// if (strongSelf)
// {
// NSLog(@"123");
// }
// else
// {
// return;
// }
// };
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)hideBar:(NSString *)str
{
NSLog(@"00000");
}
-(void)showBar:(NSString *)str
{
NSLog(@"00000");
}
-(void)close
{
}
@end