1

I have an issue where I'm trying to view a document (Word document, image, or video) that I have stored locally. I'm using UIWebView and on the Simulator it works perfectly, but on the device, it's a blank screen (no errors thrown in console). Here's my code to view the document:

    UIWebView *newWebView;
if (appDelegate.IS_IPAD)
{
    newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
}
else
{
    newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
}
newWebView.scalesPageToFit = YES;    
[self setWebView: newWebView];
[newWebView release], newWebView = nil;

[[self webView] setDelegate: self];
[[self view] addSubview:[self webView]];
NSURL *nsURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
[[self webView] loadRequest:[NSURLRequest requestWithURL:nsURL]];

Here's how I'm saving the document locally before viewing. I do think the file is getting successfully saved on the device as it does on the simulator.

    // Save the file on the device
    NSURL *contentUrl = [NSURL URLWithString:selectedContent.contentUrl];
    NSString *fileName = [[contentUrl absoluteString] lastPathComponent];       
    NSString *homeDirectoryPath = NSHomeDirectory();  // Create the path
    NSString *unexpandedPath = [homeDirectoryPath stringByAppendingString:@"/MyApp/"];
    NSString *folderPath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedPath stringByExpandingTildeInPath]], nil]];
    NSString *unexpandedImagePath = [folderPath stringByAppendingFormat:@"/%@", fileName];
    NSString *filePath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedImagePath stringByExpandingTildeInPath]], nil]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath isDirectory:NULL])
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:nil withIntermediateDirectories:NO attributes:nil error:nil];        
    }
    // Do the actual write of the file to the device
    NSData *fileData = [NSData dataWithContentsOfURL:myUrl]; // Create the file data reference
    [fileData writeToFile:filePath atomically:YES]; //Save the document to the home directory
Phamer
  • 207
  • 2
  • 7

2 Answers2

2

Watch out for case issues. The simulator is not case sensitive whereas the iPhone is!

Fran Sevillano
  • 8,103
  • 4
  • 31
  • 45
  • Thanks, I'm pretty sure it can't be case issues as I'm using the exact same value for "filePath" in --- [NSURL fileURLWithPath:filePath isDirectory:NO]; --- as I am when saving the content locally. I've even printed out the value of filePath in NSLog and it is looks fine as far as case is concerned. Anything else to be aware of besides case? – Phamer Nov 12 '11 at 00:05
  • take a look at my question about it http://stackoverflow.com/questions/6957216/load-local-image-into-uiwebview – Piotr Nov 14 '11 at 08:27
  • +1 for the case issues, that was just a problem of "assets/Media..." instead of "assets/media...", all that in an auto generated javascript... – Dulgan Oct 01 '12 at 08:44
0

I solved this by saving my document to the default Documents Directory. Here's the code I used to accomplish this:

    NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = [arrayPaths objectAtIndex:0];
    NSString *filePath = [docDirectory stringByAppendingString:@"/"];
    filePath = [filePath stringByAppendingString:fileName];
    NSData *fileData = [NSData dataWithContentsOfURL:myUrl];
    [fileData writeToFile:filePath atomically:YES];
Phamer
  • 207
  • 2
  • 7