當(dāng)在一個(gè)頁面觸發(fā)一個(gè)事件跳轉(zhuǎn)到一個(gè)H5頁面時(shí),H5頁面內(nèi)部也可以進(jìn)行下一級(jí)的跳轉(zhuǎn),這個(gè)時(shí)候我們導(dǎo)航欄使用蘋果原生的返回按鈕就需要處理頁面中的返回效果片酝。是返回到pushH5的上一級(jí)頁面還是返回H5內(nèi)部的上一級(jí)頁面?
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * backButton = [UIButton buttonWithType:UIButtonTypeSystem];
backButton.frame = CGRectMake(10, 20, 20, 20);
[backButton setBackgroundImage:[UIImage imageNamed:@"goBackImage"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.webView =[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
_webView.backgroundColor = [UIColor clearColor];
for (UIView * views in [self.webView subviews]) {
if ([views isKindOfClass:[UIScrollView class]]) {
//去掉水平方向的滑動(dòng)條
[(UIScrollView *)views setShowsHorizontalScrollIndicator:NO];
//去掉垂直方向的滑動(dòng)條
[(UIScrollView *)views setShowsVerticalScrollIndicator:NO];
for (UIView * inScrollView in views.subviews) {
if ([inScrollView isKindOfClass:[UIImageView class]]) {
//隱藏上下滾動(dòng)出邊界時(shí)的黑色的圖片
inScrollView.hidden = YES;
}
}
}
}
NSString * urlString = [NSString stringWithFormat:@"%@xxxxxxxx",BASE_URL];
NSURL * url = [NSURL URLWithString:urlString];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
_webView.delegate = self;
[_webView loadRequest:request];
[self.view addSubview:self.webView];
}
//用蘋果自帶的返回鍵按鈕處理如下(自定義的返回按鈕)
- (void)back:(UIBarButtonItem *)btn
{
if ([self.webView canGoBack]) {
[self.webView goBack];
}else{
[self.view resignFirstResponder];
[self.navigationController popViewControllerAnimated:YES];
}
}
//如果是H5頁面里面自帶的返回按鈕處理如下:
#pragma mark - webViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString * requestString = [[request URL] absoluteString];
requestString = [requestString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//獲取H5頁面里面按鈕的操作方法,根據(jù)這個(gè)進(jìn)行判斷返回是內(nèi)部的還是push的上一級(jí)頁面
if ([requestString hasPrefix:@"goback:"]) {
[self.navigationController popViewControllerAnimated:YES];
}else{
[self.webView goBack];
}
return YES;
}
//獲取當(dāng)前頁面的title和url
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[SVProgressHUD dismiss];
NSString * title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];//獲取當(dāng)前頁面的title
self.title = title;
//獲取當(dāng)前網(wǎng)頁的html
self.currentURL = webView.request.URL.absoluteString;
NSLog(@"title-%@--url-%@--",self.title,self.currentURL);
NSString *lJs = @"document.documentElement.innerHTML";
self.currentHTML = [webView stringByEvaluatingJavaScriptFromString:lJs];
}