【问题标题】:How to use UIProgressView while loading of a UIWebView?加载 UIWebView 时如何使用 UIProgressView?
【发布时间】:2009-12-14 10:39:03
【问题描述】:

我正在开发一个应用程序,我在 UIWebView 中加载一个 urlrequest 并且它成功发生了。

但是现在我试图在加载过程中显示一个 UIProgressView(从 0.0 到 1.0),它会随着加载的进度而动态变化。

我该怎么做?

【问题讨论】:

    标签: iphone uiprogressview


    【解决方案1】:

    UIWebView 在正常模式下不会为您提供任何进度信息。您需要做的是首先使用 NSURLConnection 异步获取数据。 当 NSURLConnection 委托方法 connection:didReceiveResponse 时,您将获取从 expectedContentLength 获得的数字并将其用作最大值。然后,在委托方法 connection:didReceiveData 中,您将使用 NSData 实例的 length 属性来告诉您进度,因此您的进度分数将为 length / maxLength ,标准化为 0.0 和 1.0 之间.

    最后,您将使用数据而不是 URL 来初始化 webview(在您的 connection:didFinishLoading 委托方法中)。

    两个警告:

    1. NSURLResponse 的 expectedContentLength 属性可能会是 -1NSURLReponseUnknownLength 常量)。在这种情况下,我建议您在 connection:didFinishLoading 中关闭一个标准 UIActivityIndi​​cator。

    2. 确保在任何时候从 NSURLConnection 委托方法之一操作可见控件时,都通过调用 performSelectorOnMainThread: 来实现 - 否则您将开始遇到可怕的 EXC_BAD_ACCESS 错误。

      李>

    使用此技术,您可以在知道应该获取多少数据时显示进度条,并在您不知道时显示微调器。

    【讨论】:

    • 在 UIWebView 加载请求之上实现 NSURLConnection 数据获取的问题在于,您要抓取相同的数据两次,这在轻量级连接上是浪费的。您可以使用loadData:MIMEType:textEncodingName:baseURL: 将 NSData 实例直接加载到 UIWebView 中,但这可能会损害使用 UIWebView 的其他方面。
    【解决方案2】:

    您可以尝试使用 UIWebView 的这个子类,它使用私有 UIWebView 方法 - 因此,这个解决方案不是 100% AppStore 安全的(尽管有些应用程序几乎 100% 使用它:Facebook、Google 应用程序......)。

    https://github.com/petr-inmite/imtwebview

    【讨论】:

      【解决方案3】:

      使用 NSURLConnection 会两次获取相同的数据,这是浪费时间,因为它会减慢用户交互速度,它会两次加载数据,消耗互联网数据。最好根据计时器和何时进行 uiprogress webview 成功加载了网页,它将显示 uiprogress 加载。在这种情况下,您可以在每次加载网页时显示动态 uiprogress.. 不要忘记创建一个 uiprogress 视图并将其命名为 myProgressview 并将其设置在文件所有者中。

      这里是代码希望它有帮助

      @synthesize myProgressView;
      - (void)updateProgress:(NSTimer *)sender
      {      //if the progress view is = 100% the progress stop
          if(myProgressView.progress==1.0)
          {
              [timer invalidate]; 
          }
         else
               //if the progress view is< 100% the progress increases
             myProgressView.progress+=0.5;
      }
      - (void)viewDidLoad
      { //this is the code used in order to load the site
          [super viewDidLoad];
          NSString *urlAddress = @"http://www.playbuzz.org/";
          myWebview.delegate = self;
         [myWebview loadRequest:[NSURLRequest requestWithURL:[NSURL     URLWithString:urlAddress]]];
      }
      - (void)didReceiveMemoryWarning
      {
          [super didReceiveMemoryWarning];
          // Dispose of any resources that can be recreated.
      }
      
      - (void)webViewDidFinishLoad:(UIWebView *)webView
      {    ///timer for the progress view
      
       timer=[[NSTimer scheduledTimerWithTimeInterval:0.1
                                target:self
                                selector:@selector(updateProgress:)
                                userInfo:myProgressView
                                repeats:YES]retain];
      
      }
      
      - (void)dealloc {
          [myProgressView release];
          [super dealloc];
      }
      @end
      

      这个代码和想法真的帮助我解决了我的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-15
        • 1970-01-01
        • 1970-01-01
        • 2012-11-13
        • 2012-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多