小知识点好东西

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

2017-06-12  本文已影响394人  flowerflower

需求:

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


Snip20170612_5.png
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];
            });
        };
}
2.在其需要处导入上面写的类名,然后进行初始化,push过去即可。
上一篇 下一篇

猜你喜欢

热点阅读