2012-05-09 7 views

答えて

0

このコードは、表示名と連絡先の名前と一緒にあなたのメールを提供します
- (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

これら2つのファイルをインポートすることを確認してください:

#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を取得します。

とまたあなたのプロジェクトでは、これら2つのフレームワークを追加します。

AddressBook.framework 
AddressBookUI.framework 
関連する問題