2014-06-09 20 views
10

Mam aplikację, w której mogę pobrać plik pdf i zapisać go na iPhonie. Sprawdził się doskonale w systemie iOS 7 (obecnie korzystam z systemu iOS 8) i podczas uruchamiania aplikacji w systemie iOS 8, webView pozostaje pusty. Nie mam pojęcia, co jest nie tak.UIWebView pokazuje pusty ekran w 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

Więc która część stanowi problem? Czy plik 'filePath' jest poprawny? Dlaczego masz tyle asynchronicznych bloków? Co jeśli wypróbujesz swój kod bez wszystkich bloków asynchronicznych? (Wydaje się, że nie robisz niczego, co faktycznie ich potrzebuje.) –

+0

Wszystko działało idealnie, tak jak w iOS 7. Nie wiem, czy coś zmieniło się w iOS 8, który nie może go uruchomić. W rzeczywistości bloki asynchroniczne są ogromną pomocą przy ładowaniu plików pdf. –

+1

Sugeruję, że może być konieczne wykonanie pewnych czynności związanych z debugowaniem, aby dowiedzieć się, gdzie jest problem. Może to być błąd w systemie iOS 8 lub może to być błąd w kodzie (przypadkowo zadziałał w systemie iOS 7). –

Odpowiedz

5

To jest błąd iOS 8! Możesz obejść to, używając:

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

Ładowanie PDF w UIWebView renderowane puste strony w buildach beta iOS 8.
Przeszedłem testy z oficjalną wersją iOS 8.0 i teraz wszystko działa dobrze.

+0

W rzeczywistości nadal może być trochę zabawnego biznesu. Po pierwszym uruchomieniu aplikacji i wyświetleniu pliku PDF w orientacji portretowej nie pojawia się. Wtedy, gdy obrócę się w kierunku poziomym, nagle pojawia się i wszystko działa/ładuje się dobrze po tym w dowolnej orientacji. Ugh! – Bushrod

+0

Pracuję nad tym przy użyciu setOrientation: (przez objc_msgSend ... zobacz wyjaśnienie w innym miejscu) do krajobrazu, a następnie z powrotem do portretu w webViewDidFinishLoad za pierwszym razem muszę wyświetlić plik PDF ... nie jest idealny, ale przynajmniej wygrał Wygląda na całkowicie zepsutą dla użytkowników, dopóki nie pojawi się prawdziwa poprawka. – Bushrod

Powiązane problemy