尝试使用联系人的vCard表示来获取NSData对象。使用以下界面。
+ (NSData *)dataWithContacts:(NSArray *)contacts
error:(NSError * _Nullable *)error
在将一个CNContact传递给上面的接口时获得了一个NSData对象。这是工作代码
CNMutableContact * contact = [CNMutableContact new];
contact.middleName = @"Stalin";
contact.contactType = CNContactTypePerson;
contact.givenName = @"Gates";
contact.familyName = @"GemmyApps";
NSData *bufferedData = [CNContactVCardSerialization dataWithContacts:contact error:nil];
在将CNContacts的NSArray传递给上述接口时无法获取NSData对象。这是代码
CNContactStore* contactStore = [[CNContactStore alloc]init];
NSArray * keysToFetch =@[CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey];
NSMutableArray *arrFetchedcontact = [contactStore unifiedContactsMatchingPredicate:[CNContact predicateForContactsInContainerWithIdentifier: @[contactStore.defaultContainerIdentifier][0]] keysToFetch:keysToFetch error:nil];
NSData *bufferedData = [CNContactVCardSerialization dataWithContacts:arrFetchedcontact error:nil];
lldb消息: 将联系人写入vCard(数据)的异常:获取联系人时未请求属性。
答案 0 :(得分:0)
这样你就可以通过传递数据来获取数据,
CNMutableContact * contact = [CNMutableContact new];
contact.middleName = @"Stalin";
contact.contactType = CNContactTypePerson;
contact.givenName = @"Gates";
contact.familyName = @"GemmyApps";
CNMutableContact *contact2 = [CNMutableContact new];
contact.middleName = @"Stalin1";
contact.contactType = CNContactTypePerson;
contact.givenName = @"Gates1";
contact.familyName = @"GemmyApps1";
NSArray *contactArr = [NSArray arrayWithObjects:contact,contact2,nil];
NSData *bufferedData = [CNContactVCardSerialization dataWithContacts:contactArr error:nil];
NSLog(@"data length %d",bufferedData.length);
NSLog(@"contact is %@",contact);