活动指标未在PDF uiwebview加载中显示

时间:2014-04-04 11:51:57

标签: ios objective-c uiwebview uiactivityindicatorview

我正在开发一个带有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

1 个答案:

答案 0 :(得分: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];
}