2

I've got an app which saves images (JPEGs) and text files in the app's documents directory. I have tested it in the xcode simulator and on two iPhones (4 and 5, running ios 7.1 and 8.1 respectively), and it works exactly as expected, and the data is preserved fine.

However after submitting it to the app store a user testing the app (using iphone 6, ios 8.1) has found that the saved data is being lost every 20 minutes or so. Does anyone know why this is and how I can solve the issue?

Would marking the files as Do Not Backup solve the issue?

For reference, data being saved in NSUserDefaults is being preserved.

Thanks in advance.

EDIT----

I should have mentioned that I am searching for the documents directory by using:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
self.docsPath = [paths objectAtIndex:0];

I am then appending self.docsPath with the string attributed to the image/file, XXXX.jpg:

self.imgPath = [self.fileName stringByAppendingString:@".jpg"];
self.tempPhotoPath = [self.docsPath stringByAppendingPathComponent:self.imgPath];

I should also clarify that the app functions normally on the iPhone 4/5, unplugged from xcode and running appstore downloaded versions of the app. So far in investigating, it is specifically the iPhone 6 that I am having the problems with, and from what I can tell it is simply clearing the documents directory every 20 minutes or so.

  • Are you saving the full path in to the file or relative paths? In iOS 8 the path to the document directory can change, at least the UDID of the app in the path can change. Just don't save the fun path and it should be fine. – rckoenes Feb 26 '15 at 10:59
  • I'm saving the relative filepath, so I save XXXX.jpg, and then I save the image by searching for the documents directory and appending the "path" to it with XXXX.jpg. – Stephen Betts Feb 26 '15 at 14:38
  • Post you code, maybe there is something wrong, now it is just guess work. – rckoenes Feb 26 '15 at 14:46
  • Edited post with some code to clarify the issue. – Stephen Betts Feb 26 '15 at 16:08

1 Answers1

5

in Apple new documents, whenever app launches every time it generate new sandbox id. So, it you have saved image with full path then it will might lost in second app launch. It will be not showing this effect if you test on simulator or device connected to xcode. Just plug out and run ur app, you can also see this issue your self.

So, instead of save image with its path, just create a folder in Document directory and save your image at there. For path generate you can write below code: (But make sure you save image with a specific id, like imageID and then fetch same image from that imageID).

- (NSString *)documentsPathForFileName:(NSString *)name folder:(NSString*)folderName{
    return [[self pathToPatientPhotoFolder:folderName] stringByAppendingPathComponent:name];
}

- (NSString *)pathToPatientPhotoFolder:(NSString *)folderName {
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                        NSUserDomainMask,
                                                                        YES) lastObject];
    NSString *patientPhotoFolder = [documentsDirectory stringByAppendingPathComponent:folderName];

    // Create the folder if necessary
    BOOL isDir = NO;
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    if (![fileManager fileExistsAtPath:patientPhotoFolder
                           isDirectory:&isDir] && isDir == NO) {
        [fileManager createDirectoryAtPath:patientPhotoFolder
               withIntermediateDirectories:NO
                                attributes:nil
                                     error:nil];
    }
    return patientPhotoFolder;
}

NSURL *urla = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[self.imagearray objectAtIndex:indexPath.item]]];// imagearray for array of image url from server
NSString *thumbnailCacheKey = [NSString stringWithFormat:@"thumb-%@",[self.imageIDarray objectAtIndex:indexPath.item]];//first try to check thumb-<my image id> is exist or not.
UIImage *image = [UIImage imageWithContentsOfFile:[self documentsPathForFileName:thumbnailCacheKey folder:@"thumb"]];//this will check thumb-<my image id> in ur sandbox
if (!image){
    UIImage *imageToSave = //
    NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"thumb"];
        // New Folder is your folder name
    NSError *error = nil;
        if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];                        
    NSString *filePath = [stringPath stringByAppendingPathComponent:thumbnailCacheKey];
        NSData *pngData = UIImageJPEGRepresentation(image, 0.8);
        [pngData writeToFile:filePath atomically:YES];
}
else{
    yourimageView.image = image;
}
Pratik Patel
  • 1,393
  • 12
  • 18