简体   繁体   中英

In which folder do i have to save a data file in a IOS app?

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

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.

与用户或iCloud不共享的文件应保存在

<Application_Home>/Library/Caches/<APP_BUNDLE_ID>/

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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM