0

I have file sharing enabled on my app and so, when you view the documents folder in iTunes, there are the Core Data sqlite files siting there just waiting to be fiddled with by the user.

I have found a few discussions on this but, surprisingly, no one seems to address the concerns I have.

Some say to 'move' them to the library folder - in a custom sub-directory, and another says just rename the files prefixing each with a period.

Both options sound lovely, but these are essential OS files! So my first question is, if you do either of these things (with NSFileManager.defaultManager() - I presume), will the app just automatically find them afterwards? ...or is there a specific 'way' in which you do (either of) them ...so that the app finds them afterward?

Any responses, if you could demonstrate using Swift rather than Objective C, that would be appreciated! Thanks, :)

Aroniousness
  • 149
  • 10
  • Keep them in the library folder. The application will only have access to files in the Document folder through iTunes. – El Tomato May 27 '16 at 14:29
  • Thanks, but as I asked, if I just move these files, will the app automatically find them without doing any other setup? ...When do you move them; just once on first launch? – Aroniousness May 27 '16 at 14:42
  • The app won't find them on their own. You tell it where to find them. NSLibraryDirectory points to the library. NSDocumentDirectory points to the documents folder. – El Tomato May 27 '16 at 15:20
  • Seriously? I know what the document pointers are named; but the question is regarding - what do I do to allow the app to find the files when I move them? And when do you do this? Just once during first launch, or overtime the app is launched...? If you don't know, please don't answer – Aroniousness May 27 '16 at 21:56
  • Right, I won't answer your question. But let me tell you something, silly. I have several dozen applications across Mac App Store and App Store. They all use SQLite to save and retrieve user settings. – El Tomato May 27 '16 at 22:15
  • Sorry El Tomato, shouldn't have been rude – Aroniousness May 27 '16 at 23:56

2 Answers2

2

Figured it out: To anyone wondering the same thing (which I have seen many doing),

In my app delegate > in the 'persistentStoreCoordinator' lazy variable, I changed the following line of code:

let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyAppName.sqlite")

to the following two lines of code:

let library = NSFileManager.defaultManager().URLsForDirectory(.LibraryDirectory, inDomains: .UserDomainMask)[0] as NSURL
let url = libary.URLByAppendingPathComponent("MyAppName.sqlite")
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Aroniousness
  • 149
  • 10
1

This would be the syntax for Swift 3 / 4:

    let library = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0] as NSURL
    let url = library.appendingPathComponent("MyAppName.sqlite")