2014-05-01 2 views
0

IOS 주소록 연락처 목록의 모든 연락처를 구문 분석하려는 코드를 작성하고 있습니다. 그것은 매우 직설적 인 것처럼 보입니다. 연락처와 해당 이메일이 목록으로 돌아옵니다. 내가 눈치IOS 주소록 목록, 모든 그룹이 아닌 하나의 그룹 만 반환

것은 내 아이폰의 연락처 목록에 여러 그룹 (I이 휴대 전화의 기본 연락처 목록을 검색 참조)해야한다는 것입니다 :에

  • 모든 페이스 북
  • 모든 Gmail에
  • 모든 내 IPhone

내 휴대 전화는 Facebook을 무시하고 내 두 목록을 내 목록에 표시합니다.

내 코드의 연락처 출력을 검토 한 결과 내 앱에서 기본적으로 "모든 Gmail"에 대한 연락처 만 표시된다는 것을 알았습니다.

'내 아이폰'에있는 모든 연락처를 표시하려면 어떻게해야합니까?

내 작업 응용 프로그램의 코드에서
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    // Request authorization to Address Book 
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL); 

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { 
     ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { 
      if (granted) { 
       // First time access has been granted, add the contact 
       [self addContactToAddressBook:addressBookRef]; 
      } else { 
       // User denied access 
       // Display an alert telling user the contact could not be added 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Contact list could not be loaded, since you denied access" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       [alert show]; 
      } 
     }); 
    } 
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { 
     // The user has previously given access, add the contact 
     [self addContactToAddressBook:addressBookRef]; 
    } 
    else { 
     // The user has previously denied access 
     // Send an alert telling user to change privacy setting in settings app 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Please grant access to your contact list in Iphone Settings" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 

-(void)addContactToAddressBook:(ABAddressBookRef)addressBook { 
    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook); 
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName); 
    //CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 
    CFIndex nPeople = CFArrayGetCount(allPeople); 

    self.allContacts = [[NSMutableArray alloc] init]; 

    // int contactIndex = 0; 
    for (int i = 0; i < nPeople; i++) { 
     // Get the next address book record. 
     ABRecordRef record = CFArrayGetValueAtIndex(allPeople, i); 
     CFStringRef firstName, lastName, companyName; 

     firstName = ABRecordCopyValue(record, kABPersonFirstNameProperty); 
     lastName = ABRecordCopyValue(record, kABPersonLastNameProperty); 
     companyName = ABRecordCopyValue(record, kABPersonOrganizationProperty); 

     NSString *fullName = nil; 
     if(firstName == nil && lastName == nil){ 
      fullName = [NSString stringWithFormat:@"%@", companyName]; 
     } else { 
      if(firstName == nil){ 
       fullName = [NSString stringWithFormat:@"%@", lastName]; 
      } if(lastName == nil){ 
       fullName = [NSString stringWithFormat:@"%@", firstName]; 
      } else { 
       fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 
      } 
     } 

     // Get array of email addresses from address book record. 
     ABMultiValueRef emailMultiValue = ABRecordCopyValue(record, kABPersonEmailProperty); 
     NSArray *emailArray = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue); 

     NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:2]; 
     dict[@"fullname"] = fullName; 
     dict[@"emailArr"] = (emailArray != nil ? emailArray : @[]); 

     // if(emailArray != nil){ 
      [self.allContacts addObject:dict]; 
     //} 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

답변

1

가 주소록에있는 모든 연락처를 가져 오려면 다음 코드를 사용 : 모든 그룹을 통해

ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, nil); 
NSMutableArray *contactRefs = [NSMutableArray array]; 
NSArray *sources = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllSources(book)); 
for (id source in sources){ 
    NSArray *refs = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(book, (__bridge ABRecordRef)(source), ABPersonGetSortOrdering())); 
    [contactRefs addObjectsFromArray:refs]; 
} 

기본적으로,이 무엇이고 반복을, 모든 그룹의 연락처를 contactRefs

에 추가합니다.
관련 문제