2012-05-09 597 views
0

访问iphone联系人列表中的每个联系人的所有电子邮件ID我是iphone.presently新手我在项目中工作,但我已经在项目的中间,因为不知道如何访问所有的电子邮件ID,用于从iPhone contactlist通过coding.if任何人知道这个概念,可以请帮我每个联系人....通过代码

回答

0

这个代码插上U与他们的显示名称和联系人姓名电邮沿太
- (NSMutableArray的*)getEmailAndPhoneOfPhoneContacts {

NSLog(@"getPhoneContacts"); 

    NSAutoreleasePool *pool= [[NSAutoreleasePool alloc] init]; 

    NSMutableArray *tempArray=[[NSMutableArray alloc] init]; 
    ABAddressBookRef iPhoneAddressBook=ABAddressBookCreate(); 
    if(!iPhoneAddressBook) 
    { 
     DLog(@"unable to open addressBook"); 
    } 
    //ABPerson *newPerson; 

    NSAutoreleasePool *innerPool=nil; 


    CFArrayRef allPeople=ABAddressBookCopyArrayOfAllPeople(iPhoneAddressBook); 
    NSMutableArray *peopleArray=[NSMutableArray arrayWithArray:(NSMutableArray*)allPeople]; 

    BOOL shouldReleasePool=NO; 

    NSInteger i; 

    for(i=0;i<[peopleArray count];i++) 
    { 

     if((i&255)==0) 
     { 
      innerPool= [[NSAutoreleasePool alloc] init]; 
      shouldReleasePool=YES; 
     } 


     ABRecordRef record=[peopleArray objectAtIndex:i]; 

     Contact *objPhoneContact=[[Contact alloc] init]; 
     objPhoneContact.contactType=STATIC_CONTACT; 




    CFStringRef prefixName=ABRecordCopyValue(record, kABPersonPrefixProperty); 
    CFStringRef firstName=ABRecordCopyValue(record, kABPersonFirstNameProperty); 
    CFStringRef middleName=ABRecordCopyValue(record, kABPersonMiddleNamePhoneticProperty); 
    CFStringRef lastName=ABRecordCopyValue(record, kABPersonLastNameProperty); 
    CFStringRef suffixName=ABRecordCopyValue(record, kABPersonSuffixProperty); 
    NSMutableString *contactname =[[NSMutableString alloc] init]; 



    // concating all the names 
    if (prefixName) { 
     [contactname appendString:[NSString stringWithString:(NSString*)prefixName]]; 
     CFRelease(prefixName); 
    } 
    if (firstName) { 
     [contactname appendString:[NSString stringWithString:(NSString*)firstName]]; 
     CFRelease(firstName); 
    } 
    if (middleName) { 
     [contactname appendString:[NSString stringWithString:(NSString*)middleName]]; 
     CFRelease(middleName); 
    } 
    if (lastName) { 
     [contactname appendString:[NSString stringWithString:(NSString*)lastName]]; 
     CFRelease(lastName); 
    } 
    if (suffixName) { 
     [contactname appendString:[NSString stringWithString:(NSString*)suffixName]]; 
     CFRelease(suffixName); 
    } 
    // if emty then get the organization property 
    if (contactname == nil || [contactname length]<1) { 
     CFStringRef orgName=ABRecordCopyValue(record, kABPersonOrganizationProperty); 
     if (orgName) { 
      [contactname appendString:[NSString stringWithString:(NSString*)orgName]]; 
      CFRelease(orgName); 
     } 

    } 
    //if still empty then assign (no name) to it 
    if (contactname == nil || [contactname length]<1) 
     [contactname setString:@"(no name)"]; 

     objPhoneContact.mContactName = [contactname stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 

     [contactname release]; 
     contactname = nil; 
     ABMutableMultiValueRef multi; 
     int multiCount=0; 




     multi=ABRecordCopyValue(record,kABPersonEmailProperty); 

     NSUInteger emailIndex_home=301; 
     NSUInteger emailIndex_work=321; 
     NSUInteger emailIndex_other=341; //~400 

     multiCount=ABMultiValueGetCount(multi); 
     if(multiCount ==0) 
     { 
      //[email protected]""; 
     } 
     else 
     { 
      for(int i=0; i < multiCount; i++) 
      { 
       ContactProperty* objEmailContactProperty=[[ContactProperty alloc] init]; 

       objEmailContactProperty.mContactPropertyString=(NSString*)ABMultiValueCopyValueAtIndex(multi, i); 
       objEmailContactProperty.mDisplayName=(NSString*)ABMultiValueCopyLabelAtIndex(multi, i); 
       objEmailContactProperty.mDisplayName=[objEmailContactProperty.mDisplayName stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"_$!<>"]]; 


       if([objEmailContactProperty.mDisplayName caseInsensitiveCompare:@"home"]==NSOrderedSame) 
       { 
        objEmailContactProperty.mContactPropId=emailIndex_home; 
        emailIndex_home++; 
       } 
       else if([objEmailContactProperty.mDisplayName caseInsensitiveCompare:@"work"]==NSOrderedSame) 
       { 
        objEmailContactProperty.mContactPropId=emailIndex_work; 
        emailIndex_work++; 
       } 
       /* 
       else if([objEmailContactProperty.mDisplayName caseInsensitiveCompare:@"other"]==NSOrderedSame) 
       { 
       objEmailContactProperty.mContactPropId=[NSString stringWithFormat:@"%d",emailIndex_other]; 
       emailIndex_other++; 
       } 
       */ 
       else 
       { 
        objEmailContactProperty.mContactPropId=emailIndex_other; 
        emailIndex_other++; 
       } 

       [email protected]"Email"; 

       [objPhoneContact.mPropertyArray addObject:objEmailContactProperty]; 
       [objEmailContactProperty release]; 
      } 
     } 

     if(multi) 
      CFRelease(multi); 

     [tempArray addObject:objPhoneContact]; 
     [objPhoneContact release]; 

     if(shouldReleasePool) 
     { 
      [innerPool drain]; 
      shouldReleasePool=NO; 
     } 

    } 
    self.mPhoneContactArray=tempArray; 
    CFRelease(iPhoneAddressBook); 
    CFRelease(allPeople); 


    [pool drain]; 
    return [tempArray autorelease]; 
} 
0

确保您导入这两个文件:

#import <AddressBook/AddressBook.h> 
#import <AddressBookUI/AddressBookUI.h> 

-(IBAction)showPeoplePickerController 
{ 

    ABAddressBookRef addressBook = ABAddressBookCreate(); 

    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 
    [menuArray removeAllObjects]; 
    if(menuArray ==nil) 
     menuArray = [[NSMutableArray alloc] init]; 

    for (int i = 0; i < nPeople; i++) 
    { 
     NSMutableDictionary *localDic=[[NSMutableDictionary alloc]init]; 
     ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i); 

     ABRecordID recordId = ABRecordGetRecordID(ref); 
     [localDic setObject:[NSString stringWithFormat:@"%d",recordId] forKey:@"Record_Id"]; 


     //get firstname 
     CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty); 
     [localDic setObject:[NSString stringWithFormat:@"%@",firstName] forKey:@"first_name"]; 

     //get lastname 
     CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty); 
     [localDic setObject:[NSString stringWithFormat:@"%@",lastName] forKey:@"last_name"]; 

     NSString *contactFirstLast = [NSString stringWithFormat: @"%@ %@",firstName,lastName]; 
     [localDic setObject:contactFirstLast forKey:@"FullName"]; 


     //get EmailIds 
     ABMutableMultiValueRef EmailIds = ABRecordCopyValue(ref, kABPersonEmailProperty); 
     CFTypeRef EmailId; 
     NSString *EmailLabel; 


     for (CFIndex i = 0; i < ABMultiValueGetCount(EmailIds); i++) { 
      EmailLabel=[NSString stringWithFormat:@"%@",ABMultiValueCopyLabelAtIndex(EmailIds,i)]; 

      if([EmailLabel isEqualToString:@"_$!<Home>!$_"]) 
      { 
       EmailId = ABMultiValueCopyValueAtIndex(EmailIds,i); 
       [localDic setObject:[NSString stringWithFormat:@"%@",EmailId] forKey:@"Email_Home"]; 

      } 
      else if([EmailLabel isEqualToString:@"_$!<Work>!$_"]) 
      { 
       EmailId = ABMultiValueCopyValueAtIndex(EmailIds,i); 
       [localDic setObject:[NSString stringWithFormat:@"%@",EmailId] forKey:@"Email_Work"]; 
       break; 
      } 
     } 


     [menuArray addObject:localDic]; 

    } 
    NSLog(@"%@",menuArray); 

} 

这样您就可以获得每个人的所有EmailId。

以及在项目中添加这两个框架:

AddressBook.framework 
AddressBookUI.framework