unable to create a person using VCard representation
我正在使用xcode 4.2开发一个应用程序,我试图使用initwithvcardrepresentation和/或abpersoncreatepeopleinsurcewithvcardrepresentation创建一个abperson,但是我找不到一个工作示例。有人能帮忙吗?
我得到一个nsstring格式的vCard….
谢谢
这里是声学答案的快速版本;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | let vCard : NSData // vcard let addressBook: ABAddressBook? = ABAddressBookCreateWithOptions(nil, nil)?.takeRetainedValue() ABAddressBookRequestAccessWithCompletion(addressBook) { granted, error in if !granted { return } let vCardData = CFDataCreate(nil, UnsafePointer<UInt8>(vCard.bytes), vCard.length) let defaultSource = ABAddressBookCopyDefaultSource(addressBook) let vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource.takeUnretainedValue(), vCardData).takeRetainedValue() as NSArray for person in vCardPeople { ABAddressBookAddRecord(addressBook, person, nil) } let isSaved = ABAddressBookSave(addressBook, nil) if isSaved{ //succesfully saved } else{ //not saved } } |
这是一个完整的例子,它完美地工作,它基于最新的iOS 8。
首先,您应该检查授权状态并请求访问权限(如果没有),然后保存vCard,只需查看下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){ NSLog(@"Authorized"); [self addVcardsIntoAddressBook:vcard]; } else{ NSLog(@"Not determined"); ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) { if (!granted){ NSLog(@"Just denied"); return; } NSLog(@"Just authorized"); [self addVcardsIntoAddressBook:vcard]; }); } |
以下是添加vCard的方法:
1 2 3 4 5 6 7 8 9 10 11 | - (void)addVcardsIntoAddressBook:(NSData *)vcard { CFDataRef vCardData = CFDataCreate(NULL, [vcard bytes], [vcard length]); 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); } ABAddressBookSave(book, NULL); |
}
我正在使用我在论坛上找到的代码:
1 2 3 4 5 6 7 | // Assuming your vCard is stored in vCardString as an NSString CFDataRef vCardData = (__bridge CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding]; ABAddressBookRef book = ABAddressBookCreate(); ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book); CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData); CFIndex index = 0; ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index); |
我正在使用它,它与vCard 2.1和3.0一起工作正常
nsstring*filename=[[nsbundle mainbundle]pathforresource:@"contacts"of type:@"vcf"];nslog(@"openning file%@",文件名);nsdata*stringdata=[nsdata datawithcontentsoffile:filename];nsstring*vcardstring=[[nsstring alloc]initWithData:stringData编码:nsutf8stringEncoding];
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | CFDataRef vCardData = (__bridge CFDataRef)[vCardString 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(vCardPeople); CFRelease(defaultSource); ABAddressBookSave(book, NULL); CFRelease(book); |