1

嗨,我是 iOS 开发的新手。我想从默认联系人应用程序中选择一个联系人。为此,我创建了一个应用程序,允许用户从 iPhone 默认联系人应用程序中选择联系人。对于 iOS 9+ 版本,我正在使用以下截图。

- (IBAction)btnAction:(id)sender {

    CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init];

    contactPicker.delegate = self;
    contactPicker.displayedPropertyKeys = (NSArray *)CNContactGivenNameKey;

    [self presentViewController:picker animated:YES completion:nil];
}

-(void) contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
    NSLog(@"Contact : %@",contact);
}

-(void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
    NSLog(@"Cancelled");
}

我还在我的 uiviewcontroller 中添加了 CNContactPickerDelegate 委托。当我执行上述代码时,它会打开联系人应用程序,但是当点击联系人时,应用程序会变为空白。

在此先感谢,任何人都可以分享您在 Objective-C 中使用 CNContactPickerViewController 的知识。

4

4 回答 4

7

问题是由以下代码引起的:

contactPicker.displayedPropertyKeys = (NSArray *)CNContactGivenNameKey;

显示的PropertyKeys 需要一个NSArray包含值的NSString值。在您的代码中,您尝试将 NSString 类型强制转换为 NSArray 并设置为此属性的值。

您需要将代码更改为:

contactPicker.displayedPropertyKeys = @[CNContactGivenNameKey];
于 2016-06-29T09:48:25.397 回答
3
#pragma mark - CNContactPickerViewController Delegate method implementation
(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
    NSMutableArray *contactNumberArray = [[NSMutableArray alloc]init];
    selectedName=[NSString stringWithFormat:@"%@",contact.givenName];
    NSLog(@"%@",selectedName);
    NSString *tempString = [NSString stringWithFormat:@"name : %@ %@ %@\n",contact.givenName, contact.familyName, contact.organizationName];
    //    // 1.  (Phone Numbers)
        tempString = [NSString stringWithFormat:@"%@phoneNumbers : ",tempString];
       // NSArray*phoneNumber = contact.phoneNumbers;
        for (CNLabeledValue *phoneNumber in contact.phoneNumbers)
        {
          CNPhoneNumber *phone = phoneNumber.value;
            tempString = [NSString stringWithFormat:@"%@<%@>",tempString,phone.stringValue];
             [contactNumberArray addObject:phone];
            selectedPhNumber=[[NSString stringWithFormat:@"%@",phone.stringValue] stringByReplacingOccurrencesOfString:@" " withString:@""];
             NSLog(@"%@",selectedPhNumber);
        }

         //2.  (Emails)
        tempString = [NSString stringWithFormat:@"%@\n Email : ",tempString];
        for (CNLabeledValue *email in contact.emailAddresses)
        {
            selectedEmail=[NSString stringWithFormat:@"%@", email.value];
            tempString = [NSString stringWithFormat:@"%@<%@>",tempString,email.value];
               [contactNumberArray addObject:email];
             NSLog(@"%@",selectedEmail);
        }
 [self sendRefferelDetailsToServer];

}
于 2017-03-15T05:15:17.537 回答
0
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts{

    NSLog(@" %@",contacts);

    CNContact *contact=[contacts objectAtIndex:0];

    NSLog(@"name = %@",contact.givenName);


}

[1]:https ://i.stack.imgur.com/9Sp1G.png使用上面的代码从多个选择中获取给定名称,

于 2017-02-09T09:30:21.750 回答
-1

评论以下行,然后重试。

//contactPicker.displayedPropertyKeys = (NSArray *)CNContactGivenNameKey;
于 2016-06-29T09:24:14.450 回答