0

I want to store data in iOS for globally, like session data in web. What is the best approach other than sqlite?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Susitha
  • 3,339
  • 5
  • 27
  • 41
  • 2
    are you trying nsuserdefault – Jitendra Jul 29 '13 at 05:28
  • 1
    My purpose is to send data from one viewcontroller to other. Is that suitable. Is it better than delegating. – Susitha Jul 29 '13 at 05:32
  • 1
    @Susitha NSUserDefaults is not what you want... On a website session data is used because there is no easy way to send data between webpages. In Objective-C you can pass data directly through methods. – Justin Meiners Jul 29 '13 at 05:34
  • @Susitha this is the wrie approch do dont worry... – Jitendra Jul 29 '13 at 05:35
  • @FaJitendraDeore to send data between VCs? No.. – Justin Meiners Jul 29 '13 at 05:36
  • @Justin PAssing data between vc is possible using NSUserDefault... – Jitendra Jul 29 '13 at 05:38
  • @FaJitendraDeore yes many things are possible, but also cumbersome, inefficient, error prone, and nonsensical. – Justin Meiners Jul 29 '13 at 05:38
  • Actual problem is if i push viewcontroller and collect some data then come back to the previous view, how can i pass data to this view controller. Its like settings screen on ios device. – Susitha Jul 29 '13 at 05:42
  • 1
    @Susitha when you push the view controller you can pass it all relevant data with a custom init or basic setters. When the child is done you can send the data to the parent with a delegate method or the parent can grab relevant data through getters. – Justin Meiners Jul 29 '13 at 05:45
  • @JustinMeiners thanks a lot. Could you give an example for send data to parent with a delegate method. – Susitha Jul 29 '13 at 05:49
  • 1
    @Susitha https://gist.github.com/narpas/6102365 let me know if you want me to add more detail – Justin Meiners Jul 29 '13 at 05:51
  • Thanks @JustinMeiners. This is actually i am looking for. Is this the code for parent vc or child vc. – Susitha Jul 29 '13 at 06:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34345/discussion-between-susitha-and-justin-meiners) – Susitha Jul 29 '13 at 06:03

5 Answers5

1

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;
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
1

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

Community
  • 1
  • 1
Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
1

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.

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
1

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"];
Jitendra
  • 5,055
  • 2
  • 22
  • 42
0

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"];
Suryakant Sharma
  • 3,852
  • 1
  • 25
  • 47