views:

553

answers:

3

Hi there! I'm writing an app that copies some contents of the bundle into the applications Document's directory, mainly images and media. I then access this media throughout the app from the Document's directory.

This works totally fine in the Simulator, but not on the device. The assets just come up as null. I've done NSLog's and the paths to the files look correct, and I've confirmed that the files exist in the directory by dumping a file listing in the console.

Any ideas? Thank you!

EDIT

Here's the code that copies to the Document's directory

NSString *pathToPublicationDirectory = [NSString stringWithFormat:@"install/%d",[[[manifest objectAtIndex:i] valueForKey:@"publicationID"] intValue]];
    NSString *manifestPath = [[NSBundle mainBundle] pathForResource:@"content" ofType:@"xml" inDirectory:pathToPublicationDirectory];
    [self parsePublicationAt:manifestPath];

    // Get actual bundle path to publication folder
    NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathToPublicationDirectory];
    // Then build the destination path
    NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", [[[manifest objectAtIndex:i] valueForKey:@"publicationID"] intValue]]];

    NSError *error = nil;
    // If it already exists in the documents directory, delete it
    if ([fileManager fileExistsAtPath:destinationPath]) {
        [fileManager removeItemAtPath:destinationPath error:&error];
    }
    // Copy publication folder to documents directory
    [fileManager copyItemAtPath:bundlePath toPath:destinationPath error:&error];

I am figuring out the path to the docs directory with this method:

- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

}

And here's an example of how I'm building a path to an image

path = [NSString stringWithFormat:@"%@/%d/%@", [self applicationDocumentsDirectory], [[thisItem valueForKey:@"publicationID"] intValue], [thisItem valueForKey:@"coverImage"]];
A: 

I don't see where documentsDirectory is defined.

NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", [[[manifest objectAtIndex:i] valueForKey:@"publicationID"] intValue]]];

Perhaps the following will do the trick

NSString *destinationPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", [[[manifest objectAtIndex:i] valueForKey:@"publicationID"] intValue]]];
falconcreek
It was defined earlier, sorry I left that part out.NSString *documentsDirectory = [self applicationDocumentsDirectory];The app would probably just crash if it wasn't defined.
Kevin Cupp
what does `parsePublicationAt:` do? is it parsing on a separate thread? on the device, it may not be finished.It will be helpful to check the error after the filemanager operations.`NSLog(@"fileManager error: %@", [error localizedDescription]);`
falconcreek
It parses some XML at the location specified. I've looked to see if filemanager was producing an error but it returns true. I can see that the files are in the documents directory, so it seems that they are copied over just fine. Thanks!
Kevin Cupp
A: 

Your copy code looks fine, and you say you're getting no errors.

But I'm intrigued by "The assets just come up as null." Are you sure you're accessing the file name later with the exact same name?

Unlike the simulator, a real iPhone has a case sensitive file system.

Steven Fisher
Ah that's interesting, good to know. It works for us when we access directly from the bundle, so we're pretty our file names are correct. Thanks though!
Kevin Cupp
A: 

It turned out to be an issue where the bundle was not being updated on the device and apparently didn't have the same set of files that the Simulator had. This blog post helped a bit: http://majicjungle.com/blog/?p=123

Basically, I cleaned the build and delete the app and installed directly to the device first instead of the simulator. Interesting stuff.

Kevin Cupp