16

The default Core Data for iOS template stores data in an .sqlite file in the NSDocumentDirectory. The file is therefor visible if the iDevice is connected to iTunes and file sharing is enabled.

I would like to change the directory to something that is not visible to the user and I would choose the NSApplicationSupportDirectory. Is this the correct directory? What do other developers use?

2
  • are you sure that it's visible? Dec 24, 2010 at 10:24
  • I see it when I connect the device to the computer. Might this be because it's a development device?
    – fabian789
    Dec 24, 2010 at 10:33

4 Answers 4

7

The best place (on the iPhone) is the Library folder, as described here:

How can I get a writable path on the iPhone?

That directory is meant for things like databases that you do not want the user to be able to see through sharing.

1
  • I now picked the library folder. Seems this is the place where Apple puts their files, too. (See Mail and Safari on Mac OS X)
    – fabian789
    Dec 25, 2010 at 12:47
6

The Application Support directory is a good place to put it, and by convention (from Mac OS X), you should use a subdirectory in Application Support named after your app. For iOS, it doesn't really matter where you put the file, since it's not user-accessible anyways, but keep in mind:

  • The Documents directory is visible in iTunes if you've enabled document sharing (or whatever it's called), as you've already found out.
  • The Caches directory is not saved when the user installs a new version of your app.

Here is a nice blog post with an implementation for getting the correct Application Support subdirectory: http://cocoawithlove.com/2010/05/finding-or-creating-application-support.html

1
  • 2
    Documents folder is user-accessible now (iOS 11)
    – derpoliuk
    Jan 26, 2018 at 15:53
1

You can start the file name with a dot (".myData.sqlite") and it will be hidden in the Documents directory. Better yet would be to put in a subfolder ".hidden/" and then you can put whatever you want in there, without worrying about the file names.

1
  • 3
    have you tried this with itunes for windows? I doubt it will work there. Dec 25, 2010 at 10:20
1

I did put my Core-Data-File into a subdirectory of the library directory.
By now I regret this decision. In the next version of my app the database will be accessible for the user again. I will rename it into database.sqlite and put a FAQ entry on my support page which tells the user the purpose of that file. 99.9% of the crashes that occur to my app occur because the database got corrupted in some way. And they can't send me that file, because they can't access it. So I can't find out what went wrong.

This is the code I used to access the private directory. It's in the LibraryDirectory, so iTunes will back it up automatically.

+ (NSString *)createAndReturnDirectory:(NSString *)path {
    BOOL isDirectory = NO;
    if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
        if (isDirectory)
            return path;
        NSError *error = nil;
        if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) {
            // do something. 
                    return nil;
        }
    }
    NSError *error = nil;
    if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) {
        // do something.
            return nil;
    }
    return path;
}

+ (NSString *)applicationPrivateDocumentsDirectory {
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [libraryPath stringByAppendingPathComponent:@"Private Documents"];
    return [self createAndReturnDirectory:path];
}
2
  • 3
    I don't think it makes sense to decide file locations based on getting meaningful crash reports. Just put the files where they make sense... if the user wouldn't be able to do anything with them, don't put them in Documents. Dec 25, 2010 at 10:30
  • I this this answer is great for the Mac, but on the iPhone the library makes more sense. Dec 27, 2010 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.