简体   繁体   中英

UIWebView displaying blank screen when loading

My app has to be able to download a file and display it. The format could be any of the major format types. I have been doing this with a UIWebView but when iOS 8 rolled out it broke this feature. I am wondering if this an issue with iOS 8 or is there a work around that I can use?

Here is my code:

//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];

     });
}

Thank you in advance.

I ran into the same problem rendering a pdf document on iOS 8.

So far, I've noticed this only for pdfs. So i check and see if the file is a pdf or not.

This is what i'm doing as a workaround.

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

I found out what the issue was in my case and so I wanted to post the answer.

I was using a direct file path:

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

When I should have been using a relative path like so:

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

They changes the file structure for iOS 8 which is why my code wasn't working since I was looking in the wrong place. I hope this helps someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM