2016-06-29 50 views
0

嗨,我是iOS開發新手。我想從默認聯繫人應用中選擇一個聯繫人。爲此,我創建了一個應用程序,允許用戶從iPhone默認聯繫人應用程序中選擇聯繫人。對於iOS 9+版本,我正在使用以下內容。如何使用CNContactPickerViewController與目標c?

- (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。

回答

-1

評論以下行並重試。

//contactPicker.displayedPropertyKeys = (NSArray *)CNContactGivenNameKey; 
6

的問題是由這個代碼導致的:

contactPicker.displayedPropertyKeys = (NSArray *)CNContactGivenNameKey; 

displayedPropertyKeys期待一個NSArray包含NSString值。在你的代碼中,你正試圖將一個NSString類型轉換爲NSArray並設置爲這個屬性的值。

您需要更改您的代碼:

contactPicker.displayedPropertyKeys = @[CNContactGivenNameKey]; 
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使用上面的代碼,以從多個選擇獲取給定的名稱,

2
#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]; 

} 
+0

試試這個它的做工精細爲了我。 – jeevan