iOS 8中UIWebView显示空白屏幕

10
我有一个应用程序,在其中我可以下载PDF并将其存储在iPhone中。在iOS 7上它完美地工作(我目前运行的是iOS 8),而在iOS 8上运行应用程序时,webView仅保持空白。我不知道出了什么问题。
#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

一切都像在iOS 7中一样完美地运行。我不知道在iOS 8中是否有什么改变,似乎无法运行它。实际上,异步块在加载PDF时非常有帮助。 - Santiago Fabregat
1
我建议你可能需要做一些调试工作,找出问题所在。这可能是iOS 8中的一个错误,也可能是你的代码中的一个错误(在iOS 7中意外地运行)。 - Greg Hewgill
@SantiagoFabregat 你有找到解决方案吗? - benkraus
3
这里有一个类似的问题得到了解答: https://dev59.com/aILba4cB1Zd3GeqPd2Xa - manjulad
问题已在最新的8.0版本中得到修复。 - Marek R
显示剩余2条评论
2个回答

5

这是一个与iOS 8相关的错误!您可以使用以下方法解决:

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
在iOS 8测试版中,将PDF加载到UIWebView中会显示一个空白页面。
我已经重新测试了官方发布的iOS 8.0版本,现在一切正常。

实际上,可能仍然存在一些有趣的问题。当我首次启动应用程序并在纵向显示PDF时,它不会出现。然后,如果我旋转到横向,它突然出现,并且在任何方向上加载/工作良好。唉! - Bushrod
我正在通过使用setOrientation:(通过objc_msgSend...请参见其他地方的说明)在webViewDidFinishLoad中先横屏然后再竖屏来解决它,第一次需要显示PDF时...虽然不是理想的解决方法,但至少在有真正的修复之前,它不会完全出现问题。 - Bushrod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接