PDF uiwebview 加载时未显示活动指示器
Posted
技术标签:
【中文标题】PDF uiwebview 加载时未显示活动指示器【英文标题】:Activity Indicator not showing on PDF uiwebview load 【发布时间】:2014-04-04 11:51:57 【问题描述】:我正在开发一个带有 uiwebview 的应用程序,并且在该 webview 中,当 uiwebview 通过 java 调用本机方法时,我需要加载本地 pdf 文件。我成功了,但是当我浏览 webisete 活动指示器时显示良好,但是当本机函数调用并且我在 uiwebview 中加载 pdf 时,它在从 url 加载 pdf 时不显示。让我在这里发布我的代码。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize webPage;
- (BOOL) shouldAutorotate
return NO;
- (void)viewDidLoad
[super viewDidLoad];
webLink = @"http://url.com/m/";
liNk = webLink;
[webPage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:liNk]]];
webPage.backgroundColor = [UIColor blackColor];
//[[webPage.subviews objectAtIndex:0] setBounces:NO];
[(UIScrollView*)[webPage.subviews objectAtIndex:0] setShowsVerticalScrollIndicator:NO];
[webPage addSubview:activity];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
webPage.delegate = self;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
if ([[[request URL] absoluteString] hasPrefix:@"ios:"])
// Call the given selector
[self performSelector:@selector(webToNativeCall)];
return NO;
return YES;
- (void)webToNativeCall
NSString *pdfurl = @"http://url.com/mypdf.pdf";
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pdfurl]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"mypdf.pdf"];
[pdfData writeToFile:filePath atomically:YES];
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webPage setUserInteractionEnabled:YES];
[webPage setDelegate:self];
[webPage loadRequest:requestObj];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@end
【问题讨论】:
我没有得到你的代码的完整流程。但是如果你使用的是 UIActivityIndicatorView,试试 [activity startAnimating];显示活动指示器和[活动停止动画];停止 感谢您的回复。我这样做了,但仍然没有显示活动指示器我什至尝试使用状态栏默认指示器,即使它不能正常工作。 【参考方案1】:尝试在 UIWebViewDelegate 方法中启动和停止活动指示器
#pragma mark - UIWebViewDelegate -
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
[activity startAnimating];
if ([[[inRequest URL] absoluteString] hasPrefix:@"ios:"])
// Call the given selector
[self performSelector:@selector(webToNativeCall)];
return NO;
return YES;
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
[activity stopAnimating];
-(void)webViewDidFinishLoad:(UIWebView *)webView
[activity stopAnimating];
【讨论】:
错误:使用未声明的标识符“请求” 我使用 [activity startAnimating];和[活动停止动画];不是仅在要打开 pdf 时才显示指示器,而是我需要的是即使 uiwebview 正在下载 pdf 时也显示指示器。无论如何谢谢 “错误:使用未声明的标识符'request'”已更正 在委托方法下面也添加 -(void)webViewDidStartLoad:(UIWebView *)webView [activityIndicator startAnimating]; 仍然和我在上一条消息中所说的一样,只显示 pdf 何时打开,但我想显示 uiwebview 何时下载 pdf。谢谢以上是关于PDF uiwebview 加载时未显示活动指示器的主要内容,如果未能解决你的问题,请参考以下文章