10

I have an app where I can download a pdf and store it in the iPhone. It worked perfectly in iOS 7 (I am currently running on iOS 8) and while running the app in iOS 8, the webView just stays blank. I have I no idea what's wrong.

#import "ISJMMisalViewController.h"
#import "SWRevealViewController.h"

@implementation ISJMMisalViewController
@synthesize activityImageView;

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];

    [self.webView addSubview:activityImageView];
    [activityImageView startAnimating];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
        NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"misalLocal.pdf"];

        //Now create Request for the file that was saved in your documents folder
        NSURL *url = [NSURL fileURLWithPath:filePath];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.webView setUserInteractionEnabled:YES];
            [self.webView setDelegate:self];
            [self.webView loadRequest:requestObj];
            [self.webView setScalesPageToFit:YES];
            [activityImageView stopAnimating];
            [activityImageView removeFromSuperview];

        });
    });

}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

    UIImage *image = [UIImage imageNamed: @"NavBarImage.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];

    self.navigationItem.titleView = imageView;

    _sidebarButton.target = self.revealViewController;
    _sidebarButton.action = @selector(revealToggle:);

    //Create the first status image and the indicator view
    UIImage *statusImage = [UIImage imageNamed:@"1.png"];
    activityImageView = [[UIImageView alloc] initWithImage:statusImage];


    //Add more images which will be used for the animation
    activityImageView.animationImages = [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@"1.png"],
                                         [UIImage imageNamed:@"2.png"],
                                         [UIImage imageNamed:@"3.png"],
                                         [UIImage imageNamed:@"4.png"],
                                         [UIImage imageNamed:@"5.png"],
                                         [UIImage imageNamed:@"6.png"],
                                         [UIImage imageNamed:@"7.png"],
                                         [UIImage imageNamed:@"8.png"],
                                         [UIImage imageNamed:@"9.png"],
                                         nil];


    //Set the duration of the animation (play with it
    //until it looks nice for you)
    activityImageView.animationDuration = 0.7;


    //Position the activity image view somewhere in
    //the middle of your current view
    activityImageView.frame = CGRectMake(self.view.frame.size.width/2 - 50, self.view.frame.size.height/2 - 100, 100, 100);

    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleNavBar:)];
    [self. webView addGestureRecognizer:gesture];

    if (!self.webView) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sin conexión"
                                                        message:@"No fue posible la descarga de datos debido a que no se pudo conectar con el servidor"
                                                       delegate: nil
                                              cancelButtonTitle: @"OK"
                                              otherButtonTitles: nil];

        [alert show];

    }


}

- (IBAction)actualizar:(id)sender {

    [self.webView addSubview:activityImageView];
    [activityImageView startAnimating];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://isjm.weebly.com/uploads/2/5/3/6/25368636/misal.pdf"]];
        NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
        NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"misalLocal.pdf"];
        [pdfData writeToFile:filePath atomically:YES];

        NSURL *url = [NSURL fileURLWithPath:filePath];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.webView setUserInteractionEnabled:YES];
            [self.webView setDelegate:self];
            [self.webView loadRequest:requestObj];
            [self.webView setScalesPageToFit:YES];
            [activityImageView stopAnimating];
            [activityImageView removeFromSuperview];

            if (!self.webView) {

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sin conexión"
                                                                message:@"No fue posible la descarga de datos debido a que no se pudo conectar con el servidor"
                                                               delegate: nil
                                                      cancelButtonTitle: @"OK"
                                                      otherButtonTitles: nil];

                [alert show];
            }
        });
    });
}

- (void)toggleNavBar:(UITapGestureRecognizer *)gesture {

    BOOL barsHidden = self.navigationController.navigationBar.hidden;
    [self.navigationController setNavigationBarHidden:!barsHidden animated:YES];

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

@end
Naga Mallesh Maddali
  • 1,005
  • 10
  • 19
  • So which part is the problem? Is `filePath` correct for your file? Why do you have so many async blocks? What if you try your code without all the async blocks? (You don't appear to do anything that actually needs them.) – Greg Hewgill Jun 09 '14 at 23:24
  • Everything worked perfectly just as it is in iOS 7. I don´t know if something changed in iOS 8 that can´t seem to run it. Actually the async blocks are a huge help when it comes to loading the pdfs. – Santiago Fabregat Jun 09 '14 at 23:29
  • 1
    What I'm suggesting is that you might need to do some debugging work to find out where the problem is. It might be a bug in iOS 8, or it might be a bug in your code (that accidentally happened to work in iOS 7). – Greg Hewgill Jun 09 '14 at 23:38
  • This is an ios8 bug. I have developed a html5 game which runs perfectly on ios7. It is already listed in appstore. But in ios8, it just opens blank. – Sanmoy Jul 05 '14 at 18:29
  • 3
    there is a similar question answered here http://stackoverflow.com/questions/25256905/can-not-open-pdf-file-using-uiwebview-in-ios8beta5 – manjulad Aug 23 '14 at 14:24
  • issue is fixed in latest release of 8.0. – Marek R Sep 19 '14 at 10:01

2 Answers2

5

It's an iOS 8 bug! You can work around it using:

NSString *string = [[NSBundle mainBundle] pathForResource:@"Low Cost Perimeter Build Guide" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:string];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:url];
[self.webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
1

Loading PDF in a UIWebView rendered a blank page in the iOS 8 beta builds.
I re-tested with the official iOS 8.0 release and it's working fine now.

Bushrod
  • 417
  • 3
  • 7
  • Actually, there may still be some funny business going on. When I first launch the app and display a PDF in portrait, it doesn't appear. Then if I rotate to landscape, it suddenly appears and everything works / loads fine after that in any orientation. Ugh! – Bushrod Sep 18 '14 at 23:07
  • I'm working around it by using setOrientation: (via objc_msgSend... see explanation elsewhere) to landscape and then back to portrait in webViewDidFinishLoad the first time I need to display a PDF... not ideal, but at least it won't appear completely broken to users until there's a real fix. – Bushrod Sep 18 '14 at 23:47