关于 ios:PDF uiwebview 加载时未显示活动指示器

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


尝试在 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];
}