/*
1. OC 调用 JS 只需在所需要传值的地方获取到JS中的点击方法即可
例如: 点击方法是下面的这个 functionAction
*/
- (void)functionAction {
//如果需要传递参数 给 JS,则初始化参数
NSString *str2 = @"1234mkfg";
NSString *str3 = @"kdsfj";
//传参数
[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"func('%@', '%@');", str2, str3]];
}
- 用苹果自带的返回键按钮处理如下(自定义的返回按钮)
- (void)back:(UIBarButtonItem *)bon{
if ([self.webView canGoBack]) {
[self.webView goBack];
}else{
[self.view resignFirstResponder];
[self.navigationController popViewControllerAnimated:YES];
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString * requestString = [[request URL] absoluteString];
requestString = [requestString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//获取H5页面里面按钮的操作方法,根据这个进行判断返回是内部的还是push的上一级页面
if ([requestString hasPrefix:@"goback:"]) {
[self.navigationController popViewControllerAnimated:YES];
}else{
[self.webView goBack];
}
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[SVProgressHUD dismiss];
NSString * title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];//获取当前页面的title
self.title = title;
//获取当前网页的html
self.currentURL = webView.request.URL.absoluteString;
NSLog(@"title-%@--url-%@--",self.title,self.currentURL);
NSString *lJs = @"document.documentElement.innerHTML";
self.currentHTML = [webView stringByEvaluatingJavaScriptFromString:lJs];
}
网友评论