【问题标题】:CNContact Example of a Fetch and import of a Contact into AppCNContact 获取联系人并将其导入应用程序的示例
【发布时间】:2016-01-09 21:17:44
【问题描述】:

我一直在使用ABPeople Picker 来显示联系人,然后将选定的联系人导入我的应用程序。

需要迁移到CNContact,因为AB 变得不可靠。

我找到了一些示例,但它们都在 Swift 中。具体来说,需要 CNContactPickerViewController 的 ObjectiveC 帮助。

当前代码如下所示:

-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person 
{
    CFTypeRef generalCFObject = ABRecordCopyValue(person, kABPersonFirstNameProperty);

    if (generalCFObject) {
        self.first = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);

        NSLog (@"First Name %@",first);
    }
}

【问题讨论】:

    标签: ios xcode cncontact cncontactstore cncontactviewcontroller


    【解决方案1】:

    您可以通过以下代码来做到这一点:

    -(void)selectContactData {
    
        CNContactPickerViewController * picker = [[CNContactPickerViewController alloc] init];
    
        picker.delegate = self;
        picker.displayedPropertyKeys = (NSArray *)CNContactGivenNameKey;
    
        [self presentViewController:picker animated:YES completion:nil];
    
    }
    
    -(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
    
        [self dismissViewControllerAnimated:YES completion:nil];
        NSString *test = contact.givenName;
        NSLog(@"%@",test);
    
    }
    

    显示的属性键可以是电子邮件的CNContactEmailAddressesKey。等等

    要选择多个联系人,请在上述代码中使用contactPicker:didSelectContacts: 而不是contactPicker:didSelectContact:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 2012-08-10
      • 2012-02-05
      • 1970-01-01
      相关资源
      最近更新 更多