并且可以从服务器中下载备份文件复苏到手机的功用,用系统通信录程序打开即可复苏至手机

新近在做iOS系统通讯录备份到服务器,并且可以从服务器中下载备份文件恢复生机到手机的机能,部分完毕细节记录如下。

不久前在做iOS系统通信录备份到服务器,并且可以从服务器中下载备份文件苏醒到手机的职能,部分已毕细节记录如下。

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

将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];
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文件,上传至服务器即可。

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

通信录恢复生机:

通信录复苏:

从服务器下载contents.vcf文件,用系统通信录程序打开即可恢复生机至手机。或者代码解析.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);
 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之上系统。

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

有哪些问题得以留言问我哟

有如何问题得以留言问我哟

 

 

相关文章