• 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];
    
    
        NSLog(@"%@",webView.request.URL.path);
        //与js的交互,调用js的方法名paySuccess往里面传参数
        NSString *js = [NSString stringWithFormat:@"paySuccess('%@')",@"test"];
        [webView stringByEvaluatingJavaScriptFromString:js];
    }
    

      

  • 相关阅读:
    SVN和IntelliJ IDEA忽略node_module设置
    通过出版一本案例方面的图书来包装自己(实施篇)
    在著名出版社出版书,你也行——记录我写书出版的经历和体会
    从事务角度粗窥架构的可扩展性和可维护性:内容整理自java web轻量级开发面试教程
    Hibernate(或其它ORM)里的inverse用法详解,内容摘自Java web轻量级开发面试教程
    Java web轻量级开发面试教程的前言
    根据实践经验,讲述些学习Java web能少走的弯路,内容摘自java web轻量级开发面试教程
    面试时,当你有权提问时,别客气,这是个逆转的好机会(内容摘自Java Web轻量级开发面试教程)
    通过Struts了解MVC框架,兼说如何在面试中利用Struts证明自己
    从一个简单案例上手Spring MVC,同时分析Spring MVC面试问题
  • 原文地址:https://www.cnblogs.com/karila/p/6641340.html
Copyright © 2020-2023  润新知