1

我需要以编程方式备份​​所有联系人的所有记录(号码、姓名、图像……)。我阅读了有关 AddressBook.framework 的信息,但是有没有一种简单的方法可以备份所有联系人?

4

2 回答 2

6

1.一种方法是创建一个包含所有联系人的 vCard 字符串。像这样:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef peopleForBackup  = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(peopleForBackup);
vcardString = [[NSString alloc] initWithData:(NSData *)vcards encoding:NSUTF8StringEncoding];

并将此字符串写入文件进行备份。

2.从通讯录中删除当前联系人:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
int arrayCount = CFArrayGetCount(people);
ABRecordRef abrecord;

for (int i = 0; i < arrayCount; i++) {
    abrecord = CFArrayGetValueAtIndex(people, i);
    ABAddressBookRemoveRecord(addressBook,abrecord, NULL);
}
CFRelease(people);

3.要从备份中恢复,您只需将 vcard 文件读入字符串并遍历 vcard 中的人员记录并将它们作为记录添加到地址簿,然后保存。

ABAddressBookRef addressBook = ABAddressBookCreate();
NSData *stringData = [NSData dataWithContentsOfFile:contactFilePath];
NSString *vcardFromFile = [[NSString alloc] initWithData:stringData encoding:NSUTF8StringEncoding];

CFDataRef vCardData = (CFDataRef)[vcardFromFile dataUsingEncoding:NSUTF8StringEncoding];

ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);

for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
    ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
    ABAddressBookAddRecord(addressBook, person, NULL);
    CFRelease(person);
}

CFRelease(vCardPeople);
CFRelease(defaultSource);
bool success = ABAddressBookSave(addressBook, NULL);

这种方法的一个问题是我不知道一种方法来维护联系人的链接(exchange、facebook ...)。

于 2013-01-25T08:39:47.720 回答
0

这取决于您要备份的联系人的哪些属性。如果要备份他们的名字和姓氏,请执行以下操作:

//CREATE ARRAYS STORING FIRST NAMES AND LAST NAMES
NSMutableArray *firstNames = [[NSMutableArray alloc] init];
NSMutableArray *lastNames = [[NSMutableArray alloc] init];

ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
CFRelease(addressBook);

for(NSUInteger index = 0; index <= ([arrayOfPeople count]-1); index++){

    ABRecordRef currentPerson = (__bridge ABRecordRef)[arrayOfPeople objectAtIndex: index];
    [firstNames addObject: (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty)];
    [lastNames addObject: (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonLastNameProperty)];        
}

//BACKUP THE ARRAYS IN A PLIST 
NSArray *array=[NSArray arrayWithObjects: firstNames, lastNames, nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *location = [[paths objectAtIndex:0] stringByAppendingString:@"/somefilename.plist"];
[array writeToFile:location atomically:YES];

如果您对代码有任何疑问,请在评论中提问。希望这可以帮助!

于 2012-06-28T14:22:13.157 回答