Reputation: 3349
I want to store data in iOS for globally, like session data in web. What is the best approach other than sqlite?
Upvotes: 0
Views: 865
Reputation: 9484
Use NSUserDefaults
.
NSUserDefaults is great for saving samm data like scores, login information, program state. You dont require database knowledge and its easy to learn and use.
Here is the documentation:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
Here is a good tutorial:
http://www.icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/
Edit: Although, based on your comment it seems that you just want to pass data between ViewControllers. One way :
Lets suppose you want to pass NSString
myString
from ViewControllerA
to ViewControllerB
.
Then create a property like this in ViewControllerB
.
@property (strong, nonatomic) NSString *passedString;// strong if you are using RC, ow retain
In ViewControllerA.m
, when you are allocating, initiating ViewControllerB
, then
ViewControllerB *viewControllerB = [[ViewControllerB alloc]init];
viewControllerB.passedString = myString;
Another Way: (more of a global variable type way)
You can declare a property in AppDelegate.h
@property (strong, nonatomic) NSString *passedString;
In ViewControllerB, you can create AppDelgate object and access the property:
AppDelegate *app = [[UIApplication sharedApplication]delegate];
NSString *passedString = app.passedString;
Upvotes: 1
Reputation: 9143
You can use NSUserDefaults
for that.
For saving data you can use this code
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"Dilip" forKey:@"firstName"];
[defaults setObject:@"Manek" forKey:@"lastname"];
[defaults setInteger:24 forKey:@"age"];
[defaults synchronize];
And for retrieving data use this code
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firstName = [defaults objectForKey:@"firstName"];
NSString *lastName = [defaults objectForKey:@"lastname"];
int age = [defaults integerForKey:@"age"];
NSString *ageString = [NSString stringWithFormat:@"%i",age];
You can also store NSDictionary
, NSArray
, or NSData
as object in NSUserDefault
.For more information take a look at this tutorial.
another way to pass data Between viewController is like this.
Suppose we have Two ViewController
-FirstViewController
-SecondViewController
Now if i want to pass a string from First to second ViewController thanfirst create Property of that string in secondViewcontroller
#import <UIKit/UIKit.h>
@interface SecondViewcontroller : UIViewController
@property (strong, nonatomic) NSString *strFromFirst;
@end
Synthesize it in .m file. after that in firstViewController when you push view controller Send string to second Viewcontroller
SecondViewcontroller * vc = [[SecondViewcontroller alloc] initWithNibName:@"SecondViewcontroller" bundle:nil];
// Pass the selected object to the SecondViewcontroller.
fraudluntReportViewController.strFromFirst = @"Dilip";
[self.navigationController pushViewController:vc animated:YES];
This will send the string from FirstViewController to SecondViewController.
Upvotes: 1
Reputation: 3960
If you have limited data to store feel free to use NSUserDefaults
.
Apple Reference for NSUserDefaults
for example you need to store some string(say name) in to user defaults.
NSString *name = "Your Name";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:name forKey:@"name"];
[defaults synchronize];
IMPORTANT here is, once you done with setObject for key, you have to call synchronize
method so that these changes get stored in User Defaults
for accessing the same firstName string
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[defaults objectForKey:@"name"];
Upvotes: 0
Reputation: 5081
You need to use NSUSERDefault its very easy to handle.
You can save NSString,NSDictionaray ,NSnumber in NSUserDefault like this..
// Store Array values
NSMutableArray *array = [NSMutableArray array];
[[NSUserDefaults standardUserDefaults] setObject:array forKey:@"array"];
// Stroe String to nsuserDefault
NSString *str=@"ABC";
[[NSUserDefaults standardUserDefaults] setObject:str forKey:@"value"];
Upvotes: 1
Reputation: 11113
You can write basic types like NSNumber
, NSString
, NSArray
, NSDictionary
, etc directly into the NSUserDefaults
. These will be automatically saved/loaded between app sessions.
NSString* myData = @"someValue";
[[NSUserDefaults standardUserDefaults] setObject:myData forKey:@"myData"];
For more complex data types you can take advantage of NSCoder and the NSCoding protocol to easily make your classes serializable.
The code in the answer here may be helpful. Save own Class with NSCoder
Upvotes: 1