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];

  • 相关阅读:
    2013.10.21—2013.10.25周总结
    2013.10.14—2013.10.18周总结
    2013.10.8—2013.10.12周总结
    MongoDb的“not master and slaveok=false”错误及解决方法,读写分离
    python 获取当前时间
    git命令与github使用
    s​s​h​配​置​公​钥​和​私​钥​登​陆​S​e​c​u​r​e​C​R​T
    关于pydev的语法的错误提示
    lnmp1.0 升级php.5.4.28 后出错 Nginx 502 Bad Gateway
    python线程Example
  • 原文地址:https://www.cnblogs.com/codetime/p/7001059.html
Copyright © 2011-2022 走看看