zoukankan      html  css  js  c++  java
  • ios 解决第一个页面是通过loadData或者是loadHtml时返回控制的问题

    问题: webView在通过loadData或者loadHtml方法加载到第一个页面后跳转到其他的webView后,点击导航条的返回按钮时由于webView 的canGoback为NO导致我们返回不了第一个我们通过loadHtml形式进来的web页。

    导致原因:webView 的canGoback是通过url来判定的,加入一个页面的前一个页面是通过url的形式来加载进来的,那么我们此时的canGoback为YES,否则为NO

    所以我们加入有类似一下的方法会失败

    -(void)goback{

      if([self.webView canGoback]){

           // 在通过loadHtml或者loadData加载进来的网页跳转到页面内的其他链接后返回时跳不到最初的网页

        [self.webView goback];

      }else{

        [self.navigationViewController popViewController:YES];

         }

    }

    不好的方法:通过状态来判断是加载到哪个页面然后控制goback方法,(我最开始是用这种方式尝试,发现这种方式要涉及webView的委托方法以及控制状态的值,于是放弃)。

    解决办法及分析:由于是通过loadHtml或者loadData的方式导致回不到最初的页面,而webView是由于第一个页面不是通过url加载进来的导致我们无法返回第一个web页面,那么我们就想办法将第一个页面通过url的形式加载进来那么一切又回到通过url加载页面的形式,可以让网页自己控制goforward和goback了,所以我们的方法是把拿到的html缓存到本地,然后通过localPath来创建一个url,再通过这个url来访问第一个web页,这样我们的问题就完美解决了。

    /... when we have get the data or html text 

    1. cache the data or html to local file 

    NSString *html = @"a html text";

    NSString *htmlLocalPath = @"somePath";

    [html writeToFile:htmlLocalPath atomically:YES encoding:NSUTF8Encoding error:nil];

    2.get the html via url from local path

    NSURL *url = [NSURL urlWithLocalPath:htmlLocalPath]

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    3.load html with request

    [self.webView loadRequest:request];

  • 相关阅读:
    浅谈MapReduce
    Redis源码分析(三十五)--- redis.c服务端的实现分析(2)
    Redis源码分析(三十五)--- redis.c服务端的实现分析(2)
    Redis源码分析(三十五)--- redis.c服务端的实现分析(2)
    Confluence 6 手动安装语言包和找到更多语言包
    Confluence 6 安装一个语言组件
    Confluence 6 启用主题评论
    Confluence 6 启用远程 API
    Confluence 6 配置时间和日期格式
    Confluence 6 创建-使用-删除快捷链接
  • 原文地址:https://www.cnblogs.com/codetime/p/7001059.html
Copyright © 2011-2022 走看看