2013-05-06 27 views
-1

我正在嘗試獲取地址簿聯繫人的電子郵件值。如何獲取用戶在iOS地址簿中選擇的電子郵件值?

我需要捕獲用戶點擊哪個電子郵件,我可以弄清楚如何獲得正確的語法。

我使用了一個選擇第一封電子郵件的例子,但我現在試圖將它切換到用戶選擇的電子郵件。我開始寫// NSString * email = ABRecordCopyValue(property,);

或如何獲取用戶選擇的索引值。如果我選擇了電子郵件的索引值,則可以重新使用我已註釋的代碼。

- (BOOL)peoplePickerNavigationController: 
(ABPeoplePickerNavigationController *)peoplePicker 
    shouldContinueAfterSelectingPerson:(ABRecordRef)person 
          property:(ABPropertyID)property 
          identifier:(ABMultiValueIdentifier)identifier 
{ 

    NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty); 

    self.userName.text = name; 

    self.myMessage.recpipientName = name; 

    //NSString* email = ABRecordCopyValue(property) 

    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty); 

    // if (ABMultiValueGetCount(emails) > 0) { 

    //  email = (__bridge_transfer NSString*) 

    //  ABMultiValueCopyValueAtIndex(emails, 0); 

    // } else { 

    //  email = @"[None]"; 

    // } 

    self.userEmail.text = email; 
    self.myMessage.recipientEmail = email; 
    CFRelease(emails); 
    return NO; 
} 

回答

4

這是示例代碼直接來自我的書(http://www.apeth.com/iOSBook/ch31.html#_abpeoplepickernavigationcontroller):

- (BOOL)peoplePickerNavigationController: 
     (ABPeoplePickerNavigationController *)peoplePicker 
     shouldContinueAfterSelectingPerson:(ABRecordRef)person 
     property:(ABPropertyID)property 
     identifier:(ABMultiValueIdentifier)identifier { 
    ABMultiValueRef emails = ABRecordCopyValue(person, property); 
    CFIndex ix = ABMultiValueGetIndexForIdentifier(emails, identifier); 
    CFStringRef email = ABMultiValueCopyValueAtIndex(emails, ix); 
    NSLog(@"%@", email); // do something with the email here 
    if (email) CFRelease(email); 
    if (emails) CFRelease(emails); 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    return NO; 
} 
+0

這個工作,非常感謝你! – 2013-05-08 01:31:38

相關問題