iOS webView与H5的交互(返回页面的处理)

Posted BearsG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS webView与H5的交互(返回页面的处理)相关的知识,希望对你有一定的参考价值。

当在一个页面触发一个事件跳转到一个H5页面时,H5页面内部也可以进行下一级的跳转,这个时候我们导航栏使用苹果原生的返回按钮就需要处理页面中的返回效果。是返回到pushH5的上一级页面还是返回H5内部的上一级页面?


- (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]]) 
               //去掉水平方向的滑动条
               [(UIScrollView *)views setShowsHorizontalScrollIndicator:NO];
               //去掉垂直方向的滑动条
               [(UIScrollView *)views setShowsVerticalScrollIndicator:NO];
               for (UIView * inScrollView in views.subviews) 
                    if ([inScrollView isKindOfClass:[UIImageView class]]) 
                         //隐藏上下滚动出边界时的黑色的图片
                         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页面里面按钮的操作方法,根据这个进行判断返回是内部的还是push的上一级页面
     if ([requestString hasPrefix:@"goback:"]) 
          [self.navigationController popViewControllerAnimated:YES];
     else
          [self.webView goBack];
     
     return YES;


//获取当前页面的title和url
- (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];

作者:江湖人称_赫大侠
链接:https://www.jianshu.com/p/e9b5784637c0

以上是关于iOS webView与H5的交互(返回页面的处理)的主要内容,如果未能解决你的问题,请参考以下文章

iOS前端交互篇-H5全嵌套

iOS原生App与H5页面交互笔记

Android 与 H5 交互基础普及

iOS - webView加载https

H5 与Native的交互方案

安卓Webview 与 vue h5 使用js交互