从服务器下载contents.vcf文件

近来在做iOS系统通信录备份到服务器,并且能够从服务器中下载备份文件恢复到手提式有线电话机的效应,部分完结细节记录如下。

将iphone系统通信录生成.vcf文件

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(contacts);
NSString *vcardString = [[NSString alloc] initWithData:(__bridge NSData *)vcards encoding:NSUTF8StringEncoding];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folderPath = [paths objectAtIndex:0];
NSString *filePath = [folderPath stringByAppendingPathComponent:@"contacts.vcf"];

[vcardString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

运行后系统沙盒Document目录下会生成contacts.vcf文件,上传至服务器即可。

通讯录复苏:

从服务器下载contents.vcf文件,用系统通信录程序张开即可复苏至手提式无线电话机。也许代码深入分析.vcf文件更新通信录就可以。

 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
 NSString *plistPath = [paths objectAtIndex:0];
 NSString *filePath =[plistPath stringByAppendingPathComponent:@"contacts.vcf"];
 CFDataRef vCardData = (CFDataRef)[filePath dataUsingEncoding:NSUTF8StringEncoding]; 
 ABAddressBookRef book = ABAddressBookCreate();
 ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
 CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
 for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
     ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
     ABAddressBookAddRecord(book, person, NULL);
     CFRelease(person);
 }
 CFRelease(vCardPeople);
 CFRelease(defaultSource);
 ABAddressBookSave(book, NULL);
 CFRelease(book);

留意:ABPersonCreateVCardRepresentationWithPeople只适用ios5.0以上系统。

有怎么样难点能够留言问作者哟