0

I am new to App development , I am using XCode 4.2

I am creating an application that reads QR codes . I would like to be able to save the string (NSString format) and possibly the image in a history list so that even if the user close the application the history of scanned QR codes can be retreived, how can I do that ?

a sample code will be appreciated

Thanks Alot

user1051935
  • 579
  • 1
  • 10
  • 29

3 Answers3

0

For persisting strings I would use NSUserDefaults.

NSString *QRString = stringFromQRCode;
[[NSUserDefaults standardUserDefaults] QRString forKey:@"SomeKeyToReference"];
[[NSUserDefaults standardUserDefaults] synchronize];

That will store the string in NSUserDefaults. To retrieve it later:

NSString *retrivedQRString = [[NSUserDefaults standardUserDefaults] objectForKey:@"SomeKeyToReference"];

Hope this helps.

nicholjs
  • 1,762
  • 18
  • 30
  • Oh, I wouldn't. NSUserDefaults is best for a small dictionary of settings values (and possibly for persisting those to iCloud), not for storing application data. – Dan Ray Nov 29 '11 at 15:17
  • I agree with @DanRay. NSUserDefaults' purpose is to save application settings / preferences. Not application data. – cocoahero Nov 29 '11 at 15:22
0

Consider embedding a smal SQLite database in your bundle, and using one of the open-source libraries that make SQLite interfacing easy. This is the sort of thing that could get large, and you want to use an interface that supports more or less arbitrary amounts of data.

One of my apps is a daily reminder/affirmation thing and the simplest approach was to have a SQLite database table of the daily content. Piece of cake to do, once I knew how.

Here's a list of tutorials about embedding and using SQLite: http://mobileorchard.com/iphone-sqlite-tutorials-and-libraries/

Dan Ray
  • 21,623
  • 6
  • 63
  • 87
  • 1
    If he wants to use SQLite (which in my opinion is overkill for this) then he should use Core Data. Not some random SQLite library. – cocoahero Nov 29 '11 at 15:21
  • Matter of opinion, I guess. I definitely think Core Data is overkill, but I think that SQLite on its own is lightweight and simple to use. This is a perfect use case for it, imo. – Dan Ray Nov 29 '11 at 15:46
0

If all you want to do is save a series of strings, you can do that easily with NSArray and property lists.

For example, create an NSMutableArray object. Add your strings to it, then call [myArray writeToURL:aURL atomically:NO];. That will save the plist to a file at the URL you provide.

You can then reload that list by creating an NSArray with [NSArray arrayWithContentsOfURL:aURL];

Saving the images could be done in a similar way as long the object complies with the NSCoder protocol. See here for more information.

cocoahero
  • 1,302
  • 9
  • 12