2011-08-04 39 views
3

如何從手機的地址簿中選擇某個號碼?如何從手機的地址簿中選擇某個號碼?

我從地址簿中獲得一個聯繫人,但只有手機號碼被檢索到。我應該怎麼做才能讓用戶選擇移動/家庭/其他號碼?

這裏是我的代碼:

-(IBAction)pickContact 
{ 
    // creating the picker 
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; 
    // place the delegate of the picker to the controll 
    picker.peoplePickerDelegate = self; 

    // showing the picker 
    [self presentModalViewController:picker animated:YES]; 
    // releasing 
    [picker release]; 

} 
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { 
    // assigning control back to the main controller 
    [self dismissModalViewControllerAnimated:YES]; 
} 

-(BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { 

    Contact *cont=[[Contact alloc] init]; 

    // setting the first name 
    cont.fName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 

    // setting the last name 
    cont.lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 

    // setting the number 
    /* 
    this function will set the first number it finds 

    if you do not set a number for a contact it will probably 
    crash 
    */ 



    //ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty); 

    // cont.number = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0); 
    ABMultiValueRef phones =(NSString*)ABRecordCopyValue(
                 person, kABPersonPhoneProperty); 

    CFIndex numPhoneNums = ABMultiValueGetCount(phones); 
    if(numPhoneNums == 0) { 
     NSLog(@"No number available"); 
     cont.number = @"No number available"; 
    } else { 
     cont.number = (NSString*) ABMultiValueCopyValueAtIndex(phones, 0); 
    } 

...剩下的工作好了。

回答

2

要獲得所有類型的電話號碼......(人ABRecordRef的一個實例)

NSMutableArray *arPhList = [[NSMutableArray alloc] init]; 
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); 
    for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) 
    {  
     CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j); 
     NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phones, j)); 
     NSString *phoneNumber = (NSString *)phoneNumberRef;    
     NSDictionary *dicTemp = [[NSDictionary alloc]initWithObjectsAndKeys:phoneNumber,@"value", phoneLabel,@"label", nil]; 
     [arPhList addObject:dicTemp]; 
} 
0
else { 
     cont.number = (NSString*) ABMultiValueCopyValueAtIndex(phones, **0**); 
    } 

「手機」 擁有所有的電話號碼(手機,家庭,傳真等)

但您只能使用索引。

您只使用第一個電話號碼。 (它不會移動)。

用於循環(numPhoneNums)來訪問所有數字。

相關問題