需求:
调起网页,监听返回的点击事件,返回上一层
1. 写个类,并需要导入#import <JavaScriptCore/JavaScriptCore.h>
,所需代码不上100行
- (void)viewDidLoad{
[super viewDidLoad];
UIWebView *web =[[UIWebView alloc]initWithFrame:CGRectMake(0,-0, LYScreen_Width, LYScreen_Height-64)];
web.delegate = self;
[self.view addSubview:web];
NSURLRequest *httpRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:LYlytUrl]];
[web loadRequest:httpRequest];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
JSContext *jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
LYWeakSelf;
/// 关联打印异常
jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
};
jsContext[@"closeTopicView"] = ^{ dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.navigationController popViewControllerAnimated:YES];
});
};
}