R.S*_*.S. 5 ios cncontact cncontactstore cncontactviewcontroller
我正在使用 Native CNContactViewController 添加联系人,一旦保存了联系人,它就会返回带有“:ABPerson”后缀的联系人标识符,当我在联系人列表中交叉检查时,同一联系人会显示不同的标识符。
有谁知道如何获取实际的联系人标识符?
创建代码:
- (IBAction)didSelectedAddContact:(id)sender {
CNMutableContact *contact = [CNMutableContact new];
CNContactViewController *contactController = [CNContactViewController viewControllerForNewContact:contact];
NSLog(@"contact id : %@", contact.identifier);
contactController.allowsEditing = true;
contactController.allowsActions = true;
contactController.delegate = self;
[self.navigationController pushViewController:contactController animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
委托回调:
- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact{
_contact = contact;
[viewController.navigationController popViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)
下面的函数返回 nil:
- (CNContact*) getContactFromStoreForIdentifier:(NSString*) identifier
{
CNContact *updatedContact = nil;
id descriptor = [CNContactViewController descriptorForRequiredKeys];
CNContactStore *store = [CNContactStore new];
NSError *error;
updatedContact = [store unifiedContactWithIdentifier:identifier
keysToFetch:@[descriptor]
error:&error];
// Found?
if (updatedContact == nil)
{
if (error != nil)
{
}
}
return updatedContact; }
Run Code Online (Sandbox Code Playgroud)
@Parameter:从 didCompleteWithContact 接收的 CNContact 对象的标识符:委托回调。
您必须为 viewController 设置一个 contactStore。
CNContactStore *store = [CNContactStore new];
contactController.contactStore = store;
Run Code Online (Sandbox Code Playgroud)
如果未设置此属性,则禁用将联系人添加到用户的联系人。
来源:https ://developer.apple.com/reference/contactsui/cncontactviewcontroller/1616912-contactstore