Activity Indicator not showing on PDF uiwebview load
我正在开发一个带有 uiwebview 的应用程序,并且在该 webview 中,当 uiwebview 通过 java 调用本机方法时,我需要加载本地 pdf 文件。我成功地做到了,但是当我浏览 webisete 活动指示器时显示良好,但是当本机函数调用并且我在 uiwebview 中加载 pdf 时,它在从 url 加载 pdf 时不显示。让我在这里发布我的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#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
|
- 我没有得到你的代码的完整流程。但是如果你使用的是 UIActivityIndi??catorView,试试 [activity startAnimating];显示活动指示器和[活动停止动画];停止
-
感谢回复。我这样做了,但仍然没有显示活动指示器我什至尝试使用状态栏默认指示器,即使它不能正常工作。
尝试在 UIWebViewDelegate 方法中启动和停止活动指示器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#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];
}
|
- 错误:使用未声明的标识符 \\’request\\’
-
我使用[活动开始动画];和[活动停止动画];不是仅在要打开 pdf 时才显示指示器,而是我需要的是即使 uiwebview 正在下载 pdf 时也显示指示器。无论如何谢谢
-
“错误:使用未声明的标识符 \\’request\\’ ” 已更正
-
在下面添加委托方法 -(void)webViewDidStartLoad:(UIWebView *)webView { [activityIndi??cator startAnimating]; }
-
仍然与我在上一个消息中所说的相同,仅显示 pdf 何时打开,但我想显示 uiwebview 何时下载 pdf。谢谢
-
“当 uiwebview 下载 pdf 时。”。是否下载了单独的线程。对不起,我没看懂!!
-
正如您在代码中看到的那样,它正在下载 pdf 然后将其加载到 uiwebview 中。加载 pdf 部分显示指示器,但在下载 pdf 时却没有。
-
您正在讲述这个 < NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pdfurl]]; > 。您可以在上方和下方放置开始和停止活动指示器。或者在 viewdidload 中加载视图时,您可以下载此 pdf,在使用 GCD 或 NSOperationQueue 的单独线程中,应用程序会更快
-
“initWithContentsOfURL” 是一个耗时的函数,通常在单独的线程中比在主线程中更好。如果您不熟悉 Thread、NSOperationQueue 等,您可能会觉得有点困难。应用程序只下载mypdf,文件不大,暂时可以在此功能上方启动并在下方停止使用指示器进行调整。希望这可以帮助!!
来源:https://www.codenong.com/22862057/
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_10506.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57