Question

i am developing a app with a uiwebview and in that webview i need to load a local pdf file when uiwebview calls a native method through java. and i am successful doing that but when i browse through webisete activity indicator shows well but when native function calls and i load pdf in uiwebview it doesnot shows while loading pdf from url. let me post my code here.

#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
Was it helpful?

Solution

Try starting and stoping activity indicator in UIWebViewDelegate methods

  #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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top