加载时UIWebView显示空白屏幕

时间:2014-10-23 21:44:00

标签: ios objective-c uiwebview ios8

我的应用必须能够下载文件并显示它。格式可以是任何主要格式类型。我一直用UIWebView做这个,但是当iOS 8推出时它破坏了这个功能。我想知道这是iOS 8的问题,还是我可以使用的解决方法?

这是我的代码:

//To show how I create the file path
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
//Name is the file's name that I get from the web service I use.
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:name];


- (void) showFile:(NSString *)path andFileName:(NSString *)name
{

    dispatch_async(dispatch_get_main_queue(), ^{

            self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];

            NSURL *targetURL = [NSURL fileURLWithPath:self.filePath];

            NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
            [self.webView loadRequest:request];

            self.webView.delegate = self;

            [self.view addSubview:self.webView];

     });
}

提前谢谢你。

2 个答案:

答案 0 :(得分:0)

我遇到了在iOS 8上渲染pdf文档的同样问题。

到目前为止,我已经注意到这仅适用于pdfs。所以我检查文件是否是pdf文件。

这就是我正在做的解决方法。

NSData *pdfData = [[NSData alloc] initWithContentsOfFile:fileLocation];
[self.webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];

答案 1 :(得分:0)

我发现问题出在我的案例中,所以我想发布答案。

我使用的是直接文件路径:

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

当我应该使用这样的相对路径时:

NSString *resourceDocPath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
resourceDocPath = [paths objectAtIndex:0];

他们改变了iOS 8的文件结构,这就是为什么我的代码没有工作,因为我在错误的地方找工作。我希望这有助于其他人。

相关问题