存储ABRecordRef的数组

时间:2011-08-08 13:01:06

标签: iphone-sdk-4.1 abrecordref

我们如何存储和访问ABRecordRef列表,以便我们可以在其他课程中使用它,以后再使用?

2 个答案:

答案 0 :(得分:2)

您可以从recordId类型的记录中获取ABRecordRef  addressBook然后将其转换为NSString。将其存储在NSUserDefaults

当您想要获取记录时......

recordId获取NSUserDefaults,将其转换为整数

然后使用

    ABRecordRef myRecord =
 ABAddressBookGetPersonWithRecordID(addressBook, recordId);

因此,您将获得之前保存的记录。

答案 1 :(得分:1)

试试这个:

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABRecordGetRecordID(1);  // Or whatever you're using to get the record.

// do this for each piece of information you need:
NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

// Get your receiving dict ready:
NSMutableDictionary *personDict = [NSMutableDictionary dictionary];

// then test them for a value (don't want nil values in the dictionary)
// Add each value as you test it.
if (firstName) {
    [personDict setObject:firstName forKey:@"FistName"];
}
if (lastName) {
    [personDict setObject:lastName forKey:@"LastName"];
}

// Add the personDict to NSUserDefaults:
[[NSUserDefaults standardDefaults] setObject:personDict forKey:@"MyPersonsDictionary"];

// Clean up after yourself:
[firstName release];
[lastNmae release];

应该是它。