地址簿中的电子邮件ID

时间:2012-10-01 06:58:58

标签: iphone objective-c xcode

我将地址簿中的所有联系人都收到我的申请表中,我只需要从所选联系人处获取电子邮件地址,这是我的代码

shouldContinueAfterSelectingPerson

- (BOOL)peoplePickerNavigationController:

(ABPeoplePickerNavigationController *)peoplePicker

      shouldContinueAfterSelectingPerson:(ABRecordRef)person {


    // Select phone number


    ABMultiValueRef emailProperty = ABRecordCopyValue(person, kABPersonEmailProperty);


    NSString *email = (__bridge NSString *)(emailProperty);

    [self dismissModalViewControllerAnimated:YES];

    self.view.frame = CGRectMake(0, 44, 320, 370);


    return NO;

}

请帮助我解决这些问题,提前致谢

将其打印在下面给出的日志中

电子邮件字符串是ABMultiValueRef 0x1eb2ddb0,其中包含0个值

2 个答案:

答案 0 :(得分:3)

CFStringRef value, label;
ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
CFIndex count = ABMultiValueGetCount(multi);
if (count == 1)
{
    value = ABMultiValueCopyValueAtIndex(multi, 0);
    emailid = (NSString*) value;
    NSLog(@"self.emailID   %@",emailid);
    CFRelease(value);
}
else
{
    for (CFIndex i = 0; i < count; i++)
    {
        label = ABMultiValueCopyLabelAtIndex(multi, i);
        value = ABMultiValueCopyValueAtIndex(multi, i);

        // check for Work e-mail label
        if (CFStringCompare(label, kABWorkLabel, 0) == 0)
        {
            emailid = (NSString*) value;
            NSLog(@"self.emailID   %@",emailid);
        }
        else if(CFStringCompare(label, kABHomeLabel, 0) == 0)
        {
            emailid = (NSString*) value;
            NSLog(@"self.emailID   %@",emailid);
        }

        CFRelease(label);
        CFRelease(value);
    }
}
CFRelease(multi);

答案 1 :(得分:1)

您可以添加以下代码来访问来自联系人的电子邮件..

-(IBAction)Contact 
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
        [self presentModalViewController:picker animated:YES];
    [picker release];}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    [self dismissModalViewControllerAnimated:YES];
 }

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
    ABMultiValueRef emailMultiValue = ABRecordCopyValue(person, kABPersonEmailProperty);
    NSArray *email = [(NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue) autorelease];
    CFRelease(emailMultiValue);
   [self dismissModalViewControllerAnimated:YES];}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    return NO;
}