2012-12-13 58 views
2

我想顯示一個模式的ABAddressBook只有註冊了電子郵件的聯繫人。我如何實現這一目標?彈出ABAddressBook模式只有電子郵件聯繫人

我試過這段代碼:

- (IBAction)getContact { 
    // creating the picker 
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; 

    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 

    for(CFIndex emailIndex = 0; emailIndex < nPeople; emailIndex++) { 
     ABRecordRef person = CFArrayGetValueAtIndex(allPeople, emailIndex); 
     ABMutableMultiValueRef emailRef= ABRecordCopyValue(person, kABPersonEmailProperty); 
     int emailCount = ABMultiValueGetCount(emailRef); 
     if(!emailCount) { 
      CFErrorRef error = nil; 
      ABAddressBookRemoveRecord(addressBook, person, &error); 
      if (error) NSLog(@"Error: %@", error); 
     } 
    } 
    picker.addressBook = addressBook; 
    picker.peoplePickerDelegate = self; 
    [self presentModalViewController:picker animated:YES]; 
} 

的列表顯示了我所有的接觸,但「刪除」那些看起來像「NO NAME」,並且有一個名字的人,有一個真實的電子郵件。

回答

2

我已成功地創建另一個解決方案...

我補充說,至少有一個電子郵件的觸點在陣列......取而代之的是解決方案,着眼於當前的地址簿和刪除的那些的那沒有任何電子郵件。這裏是代碼:

ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 

    for(CFIndex emailIndex = 0; emailIndex < nPeople; emailIndex++) { 
     ABRecordRef person = CFArrayGetValueAtIndex(allPeople, emailIndex); 
     ABMutableMultiValueRef emailRef= ABRecordCopyValue(person, kABPersonEmailProperty); 
     int emailCount = ABMultiValueGetCount(emailRef); 
     if(!emailCount) { 
      CFErrorRef error = nil; 
      ABAddressBookRemoveRecord(addressBook, person, &error); 
      if (error) NSLog(@"Error: %@", error); 
     } else { 
      ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty); 
      NSString *email = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0); 
      NSString *name = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); 

      if (name) { 
       NSMutableDictionary *contactDict = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
              name, @"name", 
              email, @"email", 
              nil]; 
       [self.contactsArray addObject:contactDict]; 
      } 
     } 
    } 
+0

非常感謝你:) –

相關問題