3

I have an iPad app that uses Core Data for data storage. I would like to enable file sharing in iTunes and I don't really want the users to be able to delete or modify the .sqlite file.

Can I move the file to a different, hidden directory?

Alternatively, could the file be made read-only? I wouldn't mind users having access to the file as long as it couldn't be changed.

I suspect there is a trivial solution that is escaping me at the moment.

Eric
  • 1,339
  • 1
  • 13
  • 20
  • You want to use the NSLibraryDirectory. See this answer for more information: http://stackoverflow.com/questions/1567134/how-can-i-get-a-writable-path-on-the-iphone/1567147#1567147 – Matthew Frederick Dec 29 '10 at 05:23
  • Thanks, that helps a lot. I think I am most confused about how to handle moving the file from the Documents directory. The migration itself seems like it should be trivial (just move the file before creating the persistent store in the app delegate? Then check for a valid file in each location on launch? – Eric Dec 29 '10 at 13:02

1 Answers1

2

You will want to store the core data data store in one of the recommended directories. If you need to you can then make your data store accessable to users via file sharing in the documents directory by copying it there. There is no way to restrict the user from overwriting or deleting a file or directory (via iTunes file sharing) in the documents directory.

slycrel
  • 4,275
  • 2
  • 30
  • 30
  • You do *not* want to store your database in the caches directory: the OS can delete its contents at any time. In addition, it doesn't get backed up, so if the user has to restore his device (or replace it, or whatever), the database will definitely be gone. You want the Library directory. See my comment on the question. – Matthew Frederick Dec 29 '10 at 05:24
  • the documentation I linked to above implies that you cannot use the library directory, though it might be more appropriate to use the application support directory. the documentation says: "You can call NSSearchPathForDirectoriesInDomains using a domain-mask parameter other than NSUserDomainMask or a directory constant other than those in Table 3-1, but the application will be unable to write to any of the returned directory locations." I haven't tried writing directly to the library folder, but I would expect not to be able to? I'll make my answer more generic and open ended. – slycrel Dec 29 '10 at 05:33
  • Great, thanks for the update. You can indeed write to the Library directory on the device. You may be right about `NSApplicationSupportDirectory`, I haven't tried it, but it does sound appropriate and is in the list. – Matthew Frederick Dec 29 '10 at 07:53