views:

71

answers:

1

I am developing an iPhone app with someone else. The app works fine for me, but he is running into a bug. We think this bug is related to the fact that he is getting multiple Application directories for this same app. In my ~/Library/Application Support/iPhone Simulator/User/Applications, I only have one folder at all times.

He says that he will get 3 or 4 directories when he is only working on this one app. We think this is our problem because our bug has to do with displaying images that are stored in the app's Documents folder. Does anyone know why he is ending up with multiple directories or how to stop it?

Edit:

Here is the code for writing the image to a file:

NSData *image = [NSData dataWithContentsOfURL:[NSURL URLWithString:[currentArticle articleImage]]];

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

            NSFileManager *NSFM = [NSFileManager defaultManager];
            BOOL isDir = YES;

            if(![NSFM fileExistsAtPath:imagePath isDirectory:&isDir])
                if(![NSFM createDirectoryAtPath:imagePath attributes:nil])
                    NSLog(@"error");

            imagePath = [imagePath stringByAppendingFormat:@"/images"];

            if(![NSFM fileExistsAtPath:imagePath isDirectory:&isDir])
                if(![NSFM createDirectoryAtPath:imagePath attributes:nil])
                    NSLog(@"error");

            imagePath = [imagePath stringByAppendingFormat:@"/%@.jpg", [currentArticle uniqueID]];

            [image writeToFile:imagePath atomically:NO];

And here is the code for getting the path when I need the image:

- (NSString *)imagePath
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *imagePath = [array objectAtIndex:0];
    return [imagePath stringByAppendingFormat:@"/images/%@.jpg", [self uniqueID]];
}

The app works great for me, but my partner says that the images don't show up intermittently, and he notices that he gets multiple directories in his Applications folder.

A: 

I had this problem (I was saving photos in the apps documents directory) and after every new build the directory get's renamed, so my paths were no longer valid. I cooked up these 2 functions (in my app delegate) that will give me a path for the file I want to save or load from the documents or temp directory. Even if the app directory changes, as long as you only store the file name and not the full path, and then use your helper functions to get the path when you need it later you will be ok. Here's my functions for this:

+ (NSString*)fullPathToFile:(NSString*)file {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];

return [documentsDirectory stringByAppendingPathComponent:file];
}

+ (NSString*)fullPathToTemporaryFile:(NSString*)file {
return [NSTemporaryDirectory() stringByAppendingPathComponent:file];
}

Works like a charm.

Eric Schweichler
Hmm.. we are already using this code. Maybe our problem lies elsewhere.
Alex G
Perhaps. Maybe if you post some code we might spot the problem.
Eric Schweichler
I added the code - notice anything wrong with it?
Alex G