ios - OC与HTML的交互 (OC 调用 h5 的方法)

需求:

调起网页,监听返回的点击事件,返回上一层


ios - OC与HTML的交互 (OC 调用 h5 的方法)_第1张图片
Snip20170612_5.png
1. 写个类,并需要导入#import ,所需代码不上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];
            });
        };
}
2.在其需要处导入上面写的类名,然后进行初始化,push过去即可。

你可能感兴趣的:(ios - OC与HTML的交互 (OC 调用 h5 的方法))