Show progress bar until it load the data in UIWebView IOS7

余生颓废 提交于 2019-12-08 22:28:44

问题


Hi in my application i have UIWebView which loads the pdf file using URL its very large file its taking too much time to load. Now i want show the user that like its loading progress until it get load the my pdf once my file loaded its has hide the progress bar, how can I achieve this?

My webview code.

- (void)viewDidLoad
{
   [super viewDidLoad];

   NSString *pdf =@"http://jesusredeems.com/mag/pdf/JRE-2014-03.pdf";

   NSURL *url = [NSURL URLWithString:pdf];

   NSURLRequest *myRequest = [NSURLRequest requestWithURL:url];
   [webview loadRequest:myRequest];

}

I'm using the above code to load my pdf, how I can use the progress bar for file loading?


回答1:


You should start/stop the progressbar in webview delegate methods.

Add following line in your viewDidLoad.

webview.delegate = self;

Add following functions in your controller...

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //Start the progressbar.. 
    return YES;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //Stop or remove progressbar
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    //Stop or remove progressbar and show error
}



回答2:


Yes why not, use UIWebView delegate method

- (void)viewDidLoad
{
   [super viewDidLoad];

   webview.delegate=self; //Assign Delegate

   [webview loadRequest:myRequest];

   [self showProgress];//Show Progress

}

Use delegates as below

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    if(!webView.isLoading){ //This ensures whether the webview has finished loading..
       [self hideProgress];
    }
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
   [self hideProgress];
}

Cheers.



来源:https://stackoverflow.com/questions/23966697/show-progress-bar-until-it-load-the-data-in-uiwebview-ios7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!