仅提取地址簿中的电子邮件字符

时间:2013-04-02 06:11:33

标签: ios addressbook

我使用以下代码来获取地址簿中的信息

ABRecordRef ref = CFArrayGetValueAtIndex(allPeopleName, i );
        NSString* personName = (__bridge_transfer NSString*) ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        NSDate* Date = (__bridge_transfer NSDate*) ABRecordCopyValue(ref, kABPersonBirthdayProperty);
         **NSString* personEmail = (__bridge_transfer NSString*) ABRecordCopyValue(ref, kABPersonEmailProperty);**
          NSString *birthdayDate = [dateFormat stringFromDate:Date]; 
        **NSLog(@"personEmail%@",personEmail);**

//这就是personaEmail打印的内容

personEmailABMultiValueRef 0x8a64740,含有2个值     0: $ !! $ (0x8a2bea0) - miou@yahoo.in(0x8a41df0)     1: $ !! $ (0x8a423c0) - minadi@yahoo.com(0x8a64720)

        ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
        NSString* mobile=@"";
        NSString* mobileLabel;
        for (int i=0; i < ABMultiValueGetCount(phones); i++) {
            //NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
            //NSLog(@"%@", phone);
            mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
            if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
                NSLog(@"mobile:");
            } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) {
                NSLog(@"iphone:");
            } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel]) {
                NSLog(@"pager:");
            }
            [mobile release];
            mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"%@", mobile);
            NSCharacterSet* charactersToRemove = [[NSCharacterSet decimalDigitCharacterSet] invertedSet] ;
            mobile = [[mobile componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@""] ;


    }   

现在我的问题是

  how can i get only email from that all other stuff
  i always want only one email id not two how can i get only home email id? doesnt matter how many email ids are there in address book

2 个答案:

答案 0 :(得分:0)

personEmail(或更确切地说,ABRecordCopyValue(ref, kABPersonEmailProperty)的返回值)不是NSString。查看日志消息 - 它是ABMultiValueRef

您需要使用this documentation中描述的方法来访问各个组件。

答案 1 :(得分:0)

   This will print all the email id. You can take the necessary one.

  NSString *email;
  CFStringRef value, label;
   ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
    CFIndex count = ABMultiValueGetCount(multi);
  if (count == 1)
  {
    value = ABMultiValueCopyValueAtIndex(multi, 0);
    email = (__bridge NSString*) value;
    NSLog(@"EmailID   %@",email);
    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)
        {
            email = (__bridge NSString*) value;
            NSLog(@"EmailID   %@",email);
        }

        // check for Home e-mail label
        else if(CFStringCompare(label, kABHomeLabel, 0) == 0)
        {
            email = (__bridge NSString*) value;
            NSLog(@"EmailID   %@",email);
        }

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