2014-06-09 25 views
10

Tôi có một ứng dụng mà tôi có thể tải xuống bản pdf và lưu trữ nó trong iPhone. Nó hoạt động hoàn hảo trong iOS 7 (Tôi hiện đang chạy trên iOS 8) và trong khi chạy ứng dụng trong iOS 8, webView chỉ để trống. Tôi không biết là có gì sai.UIWebView hiển thị màn hình trống trong 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

Vậy phần nào là vấn đề? FilePath' có đúng cho tệp của bạn không? Tại sao bạn có quá nhiều khối async? Điều gì nếu bạn thử mã của bạn mà không có tất cả các khối không đồng bộ? (Bạn dường như không làm bất cứ điều gì thực sự cần chúng.) –

+0

Mọi thứ hoạt động hoàn hảo giống như trong iOS 7. Tôi không biết nếu có thứ gì đó thay đổi trong iOS 8 dường như không chạy được. Trên thực tế, các khối async là một trợ giúp lớn khi tải các tệp pdf. –

+1

Điều tôi đề xuất là bạn có thể cần thực hiện một số công việc gỡ lỗi để tìm ra vấn đề ở đâu. Nó có thể là một lỗi trong iOS 8, hoặc nó có thể là một lỗi trong mã của bạn (vô tình xảy ra để làm việc trong iOS 7). –

Trả lời

5

Đó là lỗi iOS 8! Bạn có thể làm việc xung quanh nó bằng cách sử dụng:

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

Tải PDF trong UIWebView hiển thị trang trống trong bản dựng iOS 8 beta.
Tôi đã thử nghiệm lại với bản phát hành chính thức của iOS 8.0 và nó hoạt động tốt ngay bây giờ.

+0

Thực tế, vẫn có một số hoạt động kinh doanh vui nhộn đang diễn ra. Khi tôi khởi chạy ứng dụng lần đầu tiên và hiển thị tệp PDF theo chiều dọc, nó không xuất hiện. Sau đó, nếu tôi xoay để cảnh quan, nó đột nhiên xuất hiện và tất cả mọi thứ hoạt động/tải tốt sau đó trong bất kỳ định hướng. Ugh! – Bushrod

+0

Tôi đang làm việc xung quanh nó bằng cách sử dụng setOrientation: (thông qua objc_msgSend ... xem giải thích ở nơi khác) để cảnh quan và sau đó quay lại chân dung trong webViewDidFinishLoad lần đầu tiên tôi cần hiển thị PDF ... không lý tưởng, nhưng ít nhất nó đã thắng 't xuất hiện hoàn toàn bị phá vỡ cho người dùng cho đến khi có một sửa chữa thực sự. – Bushrod

Các vấn đề liên quan