views:

43

answers:

1

Hi, I am trying to use a PeoplePicker to retrieve the name and address of a contact and store it into the NSUserDefaults, which i eventually want to retrieve it on a tableview.

My question is how do I save the information in NSUserDefaults. I have used NSDictionary, but I am not making any progress. so I'm trying to save the array to the NSUserDefaults.

Can someone help me?

My code look like this:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier {

    // Only inspect the value if it's an address.
    if (property == kABPersonAddressProperty) {
        ABMutableMultiValueRef multiValue = ABRecordCopyValue(person, property);
        for(CFIndex i=0;i<ABMultiValueGetCount(multiValue);i++)
        {
            ABMultiValueRef multi = ABRecordCopyValue(person, property);

        // Set up an NSArray and copy the values in.
        NSArray *theArray = [(id)ABMultiValueCopyArrayOfAllValues(multi) autorelease];

        // Figure out which values we want and store the index.
        const NSUInteger theIndex = ABMultiValueGetIndexForIdentifier(multi, identifier);

        // Set up an NSDictionary to hold the contents of the array.
        NSDictionary *dictionary = [theArray objectAtIndex:theIndex];

        // Set up NSStrings to hold keys and values.  First, how many are there?
        const NSUInteger theCount = [dictionary count];
        NSString *keys[theCount];
        NSString *values[theCount];

        // Get the keys and values from the CFDictionary.  Note that because
        // we're using the "GetKeysAndValues" function, you don't need to
        // release keys or values.  It's the "Get Rule" and only applies to
        // CoreFoundation objects.
        [dictionary getObjects:values andKeys:keys];

        // Set the address label's text.
        NSString *address;
        address = [NSString stringWithFormat:@"%@, %@, %@",
                   [dictionary objectForKey:(NSString *)kABPersonAddressStreetKey],
                   [dictionary objectForKey:(NSString *)kABPersonAddressCityKey],
                   [dictionary objectForKey:(NSString *)kABPersonAddressCountryKey]];

        NSLog(@"%@", address);

        [self dismissModalViewControllerAnimated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    }
    NSUserDefaults *locatie = [NSUserDefaults standardUserDefaults];
    CFRelease(multiValue);
}

    return NO;
}
A: 
[locatie setObject:yourObject forKey:@"YoureKey"];

If you want to store the adress use:

[locatie setObject:adress forKey:@"adressKey"];

to retrive use:

adress = [locatie valueForKey:@"adressKey"];

NOTE: In these examples locatie is: NSUserDefaults *locatie = [NSUserDefaults standardUserDefaults];

Larsaronen
I want to select an address and then fill the array with the address i have selected and already have saved it. I don't know how to do that.
Nico
What do you mean? If you want to save/retrive from NSUserDefaults use what i mentioned above.. if you want to add an object to an array use addObject: like this:[youreArray addObject:adress];
Larsaronen
as you can see in my code i am trying to retrieve the data from the addressbook and added it into an NSArray. right now i am using [locationArray addObjectsFromArray:theArray]; NSLog(@"%@", locationArray); NSUserDefaults *locatie = [NSUserDefaults standardUserDefaults]; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:locationArray]; NSLog(@"%@", data); [locatie setObject:data forKey:@"array"]; [locatie synchronize];to add the nsarray to a nsmutablearray and put that as NSdata to the nsuserdefaults. and i can't retrieve the data, I'm receiving a (null)
Nico
Do you need to use NSKeyedArchiver? How does [locatie setObject:locationArray forKey:@"array"]; work?
Larsaronen
I have tried with the codes you give me, but I couldn't save an array into another array. so now i have to figure that out before i can try to save the array in NSUserDefaults
Nico