Question

my iOS application has some json files in its bundle ,, i've updated these files and upload to the store , but when updated the old json files data is still shows up , the only solution i found is to delete the app and install it again but its not a good solution to send to all users

so is there any way to it programtically or any other way

P.S : i tried to change the name of the json files , but its start to crash because the app can't see the path resource of the bundle with the new name here is my code to call these files

{
 NSString* aFileName = @"Models.json";

NSString * fileDocumentPath = [NSString stringWithFormat:@"%@/%@", [self getDocumentsDirectoryPath], aFileName];

NSFileManager * fileMngr = [NSFileManager defaultManager];
 NSString * file = [aFileName stringByReplacingOccurrencesOfString:@".json" withString:@""];

 NSString * sourcePath =  [[NSBundle mainBundle] pathForResource:file ofType:@"json"];

 NSError *error;
 [fileMngr copyItemAtPath:sourcePath toPath:fileDocumentPath error:&error];
}

-(NSString *) getDocumentsDirectoryPath {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    documentsDirectoryPath = [pathArray objectAtIndex:0];

    return documentsDirectoryPath;
}
Was it helpful?

Solution

You can do one thing , before copying the file just check if the file exist , if exist then delete the file and then copy the new file. This should solve your issue.

Check below link for deleting files How do I delete a file in my apps documents directory?

Delete specified file from document directory

Regards, Anil

OTHER TIPS

if ([fileMngr fileExistsAtPath:fileDocumentPath isDirectory:NO]) {
    [fileMngr removeItemAtPath:fileDocumentPath error:nil];
}

 [fileMngr copyItemAtPath:sourcePath toPath:fileDocumentPath error:&error];

Swift 3:

   func deleteFile(_ filePath:URL) {
        guard FileManager.default.fileExists(atPath: filePath.path) else {
            return
        }
        do {
            try FileManager.default.removeItem(atPath: filePath.path)
        } catch {
            print("Unable to delete file.")
        }
    }

    let fileURL = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first! + "namefile.json")
    deleteFile(fileURL)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top