3

I'm developing a QRCode reader app. When a qrcore is scanned I want to save it data in a file. I don't want to back it up in icloud, I just want that the data of file don't be lost when the app is closed.

so which folder should i save my data file?

thanks

Alessandro Garcez
  • 728
  • 2
  • 6
  • 25

3 Answers3

5

As per the iOS Data Storage Guidelines, you have options to store data in

  1. Documents - User generated data (non-reproducible)
  2. Caches - Cached content (reproducible)
  3. Temp - Temporary data (needs no persistence across sessions)
  4. Documents with no iCloud backup attribute

Decision to choose which place to store depends on the context of the app. If the QRCode file, needs to be there and is user specific opt for option 4. The data stored in caches directory can be purged if the device runs out of disk space.

NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

Please refer answer1 and answer2 for more details.

Thanks to Tommy, danh and The Tiger for making the answer more complete.

Community
  • 1
  • 1
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • 3
    And see QA1719 — http://developer.apple.com/library/ios/#qa/qa1719/_index.html — for how to flag files as not for backup. – Tommy Jun 15 '13 at 00:24
  • 2
    Or use the more correct answer which is to use NSCachesDirectory (explicitly for app generated data that's difficult to recreate and isn't to be synched with iCloud. – danh Jun 15 '13 at 03:01
  • 1
    @TheTiger Thanks for your suggestion. I would edit the answer to include these details. – Anupdas Jun 15 '13 at 04:34
1

Files which are not shared with user or iCloud should be saved in

<Application_Home>/Library/Caches/<APP_BUNDLE_ID>/
Lightygalaxy
  • 121
  • 3
0

You could also use NSDefaults, which stores the data in an XML file in the Preferences folder. To store data:

NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
[settings setInteger:qrCode forKey:@"QRCode"];
[settings synchronize];

(assuming the QR Code data is stored in an integer called qrCode) To read the QR code back again:

NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
int qrCode = [settings integerForKey:@"QRCode"];
jhiversen
  • 11
  • 1
  • This will also overwrite his previous scanned and decoded QR code. Core Data might be a better option. – Zhang Jun 15 '13 at 17:01
  • I don't know @user2488013, but using NSUserDefault don't seem to be the best practice. I have read some articles and documents, and the Coredata seem to be the best way to store the data. – Alessandro Garcez Jun 16 '13 at 12:37
  • He specified he just wanted to save the QR code so it didn't get lost when the app is closed. Core Data is seriously overkill if that's the only use for storing data. NSUserDefaults is quick and easy to set up in just a few lines of code. You're right, NSUserDefaults is supposed to be for settings, but it would work here as well. Otherwise, there's Plist as well... http://stackoverflow.com/questions/7058858/should-i-use-nsuserdefaults-or-a-plist-to-store-data – jhiversen Jun 17 '13 at 04:17