Question

right ive got this code below which will save a integer when the save button is clicked and the app is shut however i need it to do this automatically... how do I go about doing this with ios? quite new to the whole apple coding thing so any help is welcome

-(IBAction)SaveNumber:(id)sender{
    [[NSUserDefaults standardUserDefaults] setInteger:Number forKey:@"savenumber"];
}

- (void)viewDidLoad {

    Number = [[NSUserDefaults standardUserDefaults] integerForKey:@"savenumber"];
    ShowSavedNumber.Text = [NSString stringWithFormat:@"%i",Number];

    [super viewDidLoad];
}
Was it helpful?

Solution

The problem is, where you try to save this value? you don't have a file to storage this value in Documents directory inside the App.

You can save any value in your app, but you have to use a file for storage and read, or saving and load via URL on your server. Like file.plist, SqlLight, json with MySql.

So the first simple saving is using a file .plist inside your app, under the AppDelegate.m you have to check your directory if the file exist and copy inside a Documents directory (the only writable folder), but after that you have to learn how to load and save on the same file using Dictionary , MutableDictionary and some other class. I can help you to understant the first steps like a check and create a .plist, read, and first base to save, but for other thing is to much code and yopu have to watch other tutorials.

So create a .plist file and change the structure like this: enter image description here Change key has you want, but remember the Dictionary contain more string value, if you want add more Item you have to add other row inside the same Root Array.

Root = Array
Item0 > some string with value
Item1 > some string with value

Now you have to copy this file inside a writable Documents folder when the application lauching, so inside your AppDelegate.m create a function:

- (void)createPlistFile{

    NSError *error;
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourFile.plist"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path])
    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"yourFile" ofType:@"plist"];

        [fileManager copyItemAtPath:bundle toPath: path error:&error];
    }

}

Under the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { add a call: [self createPlistFile];

Ok now you have Check first if the file exist in documents folder, and if is false, the app create a new file.

Now here the standard code to read a new file inside your app:

in your ViewControlle.h write:

@interface ViewController : UIViewController {
    NSMutableArray *loadData;
}

@property (nonatomic, strong) NSMutableArray *loadData;

Come back to your ViewController.m and write:

@synthesize loadData;

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"yourFile.plist"];
    NSMutableArray *dataDict = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    self.loadData = dataDict;

Ok now you have create a new PLIST file inside a Documents Directory an correctly load, but now you have to save, this is a basic method to save, but theres really many mode for that and I can't write all here ;)

- (IBAction)save:(id)sender {

    NSMutableDictionary *nr2 = [[NSMutableDictionary alloc] init];
    [nr2 setValue:emittente.text forKey:@"plistKey"];
    [nr2 setValue:link.text forKey:@"plistKey"];
    [nr2 setValue:images.text forKey:@"plistKey"];
    [nr2 setValue:web.text forKey:@"plistKey"];

    [YouMutableDict addObject:nr2];
    [YouMutableDict writeToFile:listFav atomically:YES];
}

Ok that's all I hope this help you to start understand how to save Data on app if yes please vote ;)

OTHER TIPS

Changes made to NSUserDefaults are not automatically saved. You must call -synchronize on the NSUserDefaults object in order to save. Generally you want to do this once after you have made all of the updates you plan on performing (if in fact you need to do multiple updates).

Here is Apple's reference: http://developer.apple.com/library/ios/ipad/#documentation/cocoa/reference/foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top