2014-06-09 14 views
10

Ho un'app in cui posso scaricare un pdf e archiviarlo nell'iPhone. Ha funzionato perfettamente su iOS 7 (attualmente sono in esecuzione su iOS 8) e mentre eseguo l'app su iOS 8, il webView rimane vuoto. Non ho idea di cosa c'è che non va.UIWebView che mostra una schermata vuota in iOS 8

#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 
+0

Quindi quale parte è il problema? 'FilePath' è corretto per il tuo file? Perché hai così tanti blocchi asincroni? Cosa succede se provi il tuo codice senza tutti i blocchi asincroni? (Non sembra che tu faccia nulla che abbia realmente bisogno di loro.) –

+0

Tutto ha funzionato perfettamente come in iOS 7. Non so se qualcosa è cambiato in iOS 8 che non sembra funzionare. In realtà i blocchi asincroni sono di grande aiuto quando si tratta di caricare i pdf. –

+1

Quello che sto suggerendo è che potrebbe essere necessario fare del lavoro di debug per scoprire dove si trova il problema. Potrebbe trattarsi di un bug in iOS 8, o potrebbe essere un bug nel tuo codice (che per errore è successo a lavorare su iOS 7). –

risposta

5

È un bug di iOS 8! Puoi aggirare il problema utilizzando:

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

Caricamento PDF in UIWebView ha reso una pagina vuota nei build di iOS 8 beta.
Ho provato nuovamente con la versione ufficiale di iOS 8.0 e sta funzionando bene ora.

+0

In realtà, potrebbero ancora esserci degli affari divertenti in corso. Quando lancio l'app per la prima volta e visualizzo un PDF in verticale, non viene visualizzato. Quindi se ruoto in orizzontale, appare all'improvviso e tutto funziona/carica bene dopo quello in qualsiasi orientamento. Ugh! – Bushrod

+0

Sto lavorando attorno ad esso usando setOrientation: (tramite objc_msgSend ... vedi la spiegazione altrove) per orizzontare e poi tornare al ritratto in webViewDidFinishLoad la prima volta che ho bisogno di visualizzare un PDF ... non è l'ideale, ma almeno ha vinto sembra completamente rotto agli utenti fino a quando non c'è una soluzione reale. – Bushrod

Problemi correlati