4

我正在使用 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];    
}

委托回调:

- (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 对象的标识符:委托回调。

4

1 回答 1

0

您必须为 viewController 设置一个contactStore。

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

如果未设置此属性,则禁用将联系人添加到用户的联系人。

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

于 2017-03-14T14:03:32.347 回答