ios - 返回带 ABPerson 后缀的联系人标识符

标签 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];    
}

委托(delegate)回调:

- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact{
_contact = contact;

    [viewController.navigationController popViewControllerAnimated:YES];
}

下面的函数返回 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; }

@Parameter:从 didCompleteWithContact 接收到的 CNContact 对象的标识符:委托(delegate)回调。

最佳答案

您必须为 viewController 设置一个 contactStore。

CNContactStore *store = [CNContactStore new];
contactController.contactStore = store;

If not this property is not set, than adding the contact to the user's contacts is disabled.

来源:https://developer.apple.com/reference/contactsui/cncontactviewcontroller/1616912-contactstore

关于ios - 返回带 ABPerson 后缀的联系人标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42787422/

相关文章:

ios - 如何从 iOS 上的 native Facebook 应用程序在 Safari 中打开移动网络应用程序链接?

ios - 如何将联系人保存到 iOS 中的联系人应用程序中 - Swift

iOS9 联系人框架从新保存的联系人中获取标识符

iphone - 在 UIbutton 中分配随机标题

iphone - 当 Gesture 应用于 self.view 时,我们如何才能使 TableView 工作?

iphone - 使用 CNContactStore 以编程方式将家庭地址保存到新联系人?雨燕3

ios - SwiftUI 列表仅使用给定数组的第一项

IOS/objective-C/ Storyboard : Custom Segue from Left to Right Creating Black Bar Between View Controllers

swift - 无法从通用函数中的 CNMutableContact 中删除项目