iPhone: Can access files in documents directory in

2019-04-03 04:09发布

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

3条回答
The star\"
2楼-- · 2019-04-03 04:58

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.

查看更多
何必那么认真
3楼-- · 2019-04-03 04:59

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]]];
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-04-03 05:04

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.

查看更多
登录 后发表回答