Как сохранить информацию, извлеченную из адресной книги, в NSUserDefaults? - PullRequest
0 голосов
/ 15 сентября 2010

Я пытаюсь использовать PeoplePicker, чтобы получить имя и адрес контакта и сохранить его в NSUserDefaults, которые я в итоге хочу получить в виде таблицы.

У меня вопрос, как мне сохранить информацию в NSUserDefaults. Я использовал NSDictionary, но я не делаю никакого прогресса. поэтому я пытаюсь сохранить массив в NSUserDefaults.

Может ли кто-нибудь мне помочь?

Мой код выглядит так:

- (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;
}

1 Ответ

0 голосов
/ 15 сентября 2010
[locatie setObject:yourObject forKey:@"YoureKey"];

Если вы хотите сохранить адрес, используйте:

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

чтобы восстановить использование:

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

Примечание: В этих примерах locatie: NSUserDefaults *locatie = [NSUserDefaults standardUserDefaults];

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...