【问题标题】:Show progress bar until it load the data in UIWebView IOS7显示进度条,直到它在 UIWebView IOS7 中加载数据
【发布时间】:2014-05-31 05:24:32
【问题描述】:
嗨,在我的应用程序中,我有 UIWebView,它使用 URL 加载 pdf 文件,它的文件非常大,加载时间过长。现在我想向用户展示喜欢它的加载进度,直到它加载我的pdf,一旦我的文件加载它隐藏了进度条,我该如何实现?
我的 webview 代码。
- (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];
}
我正在使用上面的代码来加载我的pdf,如何使用进度条来加载文件?
【问题讨论】:
标签:
ios
pdf
uiwebview
progress-bar
【解决方案1】:
您应该在 webview 委托方法中启动/停止进度条。
在您的viewDidLoad 中添加以下行。
webview.delegate = self;
在您的控制器中添加以下功能...
-(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】:
是的,为什么不呢,使用UIWebView委托方法
- (void)viewDidLoad
{
[super viewDidLoad];
webview.delegate=self; //Assign Delegate
[webview loadRequest:myRequest];
[self showProgress];//Show Progress
}
如下使用委托
- (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];
}
干杯。