美文网首页小知识点好东西
ios - OC与HTML的交互 (OC 调用 h5 的方法)

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

作者: flowerflower | 来源:发表于2017-06-12 14:51 被阅读394次

需求:

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


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过去即可。

相关文章

网友评论

    本文标题:ios - OC与HTML的交互 (OC 调用 h5 的方法)

    本文链接:https://www.haomeiwen.com/subject/osrlqxtx.html