2012-05-07 124 views
0

我能夠從iphone sdk中拉出聯繫人列表的名字和姓氏,但我無法從中獲取電話號碼。我得到的錯誤,如果我嘗試別的辦法了,我越來越有此電話號碼,其他的東西通常的方法是用代碼的細節:聯繫電話號碼iphone sdk

- (IBAction)buttonmessage { 
    ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book reference object 
    NSArray *abContactArray = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); // get address book contact array 

    NSInteger totalContacts = [abContactArray count]; 

    for(NSUInteger loop= 0 ; loop < totalContacts; loop++) 
    { 
     ABRecordRef record = (__bridge ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record 

     if(ABRecordGetRecordType(record) == kABPersonType) // this check execute if it is person group 
     { 
      ABRecordID recordId = ABRecordGetRecordID(record); // get record id from address book record 

      recordIdString = [NSString stringWithFormat:@"%d",recordId]; // get record id string from record id 

      firstNameString = (__bridge NSString*)ABRecordCopyValue(record,kABPersonFirstNameProperty); // fetch contact first name from address book 
      lastNameString = (__bridge NSString*)ABRecordCopyValue(record,kABPersonLastNameProperty); // fetch contact last name from address book 

      NSString *phnumber = (__bridge NSString *)ABRecordCopyValue(record, kABPersonPhoneProperty); 

      myArray2 = [NSArray arrayWithObjects:firstNameString,lastNameString,phnumber,nil]; 

      NSString *m12=[NSString stringWithFormat:@"%@,%@,%@",[myArray2 objectAtIndex:0],[myArray2 objectAtIndex:1],[myArray2 objectAtIndex:2]]; 
     } 

輸出:

Abdullah,Rashed,ABMultiValueRef 0x80426a0 with 1 value(s) 
    0: _$!<Mobile>!$_ (0x8042da0) - 0550979691 (0x8042dc0) 
2012-05-07 14:43:06.670 Firstphase[2914:207] Hussain,Mahmood,ABMultiValueRef 0x80442d0 with 1 value(s) 
    0: _$!<Mobile>!$_ (0x8044290) - 055979896 (0x80442b0) 


2012-05-07 14:43:06.671 Firstphase[2914:207] Nasir,Jilaani,ABMultiValueRef 0x8046070 with 1 value(s) 
    0: _$!<Mobile>!$_ (0x8046000) - 055982391 (0x8046020) 


2012-05-07 14:43:06.673 Firstphase[2914:207] Ghulam,Basith,ABMultiValueRef 0x8046850 with 1 value(s) 
    0: _$!<Mobile>!$_ (0x8046810) - 055871943 (0x8046830) 

然而如果你仔細觀察,我可以得到名字和姓氏,而不需要額外的東西。但我無法以相同的方式獲得電話號碼。

+0

如果以下答案之一解決了您的問題,請將其標記爲是幫助未來網站訪問者找到類似問題的答案的答案。 – pasawaya

回答

2
ABMutableMultiValueRef multi; 
int multiCount = 0; 
multi = ABRecordCopyValue(record, kABPersonPhoneProperty); 
multiCount = ABMultiValueGetCount(multi); 
for (int i = 0; i < multiCount; i++) { 

    phoneNumber = (NSString *) ABMultiValueCopyValueAtIndex(multi, i); 
    [someArray addObject: phoneNumber]; 

} 
2

檢查:

您的問題可能與此answerthis解決。我知道ABRecordCopyValue(ref, kABPersonPhoneProperty)返回一些數組值。你試圖得到一個字符串,這就是爲什麼你可能面臨的問題。我沒有嘗試過這種解決方案,但認爲它會起作用。

希望這會有所幫助。

1

嘗試以下操作:

ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(record, kABPersonPhoneProperty); 
NSArray *phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty); 

for (id number in phoneNumbers) {      
    // do whatever you want 
} 
1

將下面的代碼從聯繫人列表檢索所有的電話號碼: -

ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 
    for (int i = 0; i < nPeople; i++) 
    { 
     ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i); 

     ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(ref, kABPersonPhoneProperty); 
     NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty); 
     CFRelease(phoneNumberProperty); 
     [phoneNumbers release]; 

    } 

它會運行

1
-(void)displayPerson 
{ 



CFErrorRef error = NULL; 


    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); 

    if (addressBook != nil) 
    { 
     NSLog(@"Succesful."); 
     NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); 

     NSUInteger i = 0; 
     for (i = 0; i < [allContacts count]; i++) 
     { 
      Person *person = [[Person alloc] init]; 
      ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i]; 
      NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty); 
      NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty); 
      NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 
      NSString *phone=nil; 
      ABMultiValueRef phoneNumbers = ABRecordCopyValue(contactPerson,                    kABPersonPhoneProperty); 
      if (ABMultiValueGetCount(phoneNumbers) > 0) { 
       phone = (__bridge_transfer NSString*) 
       ABMultiValueCopyValueAtIndex(phoneNumbers, 0); 
      } else { 
       phone = @"[None]"; 
      } 
      person.fullname = fullName; 
      person.phoneNum=phone; 
      [self.tableData addObject:person]; 
      person=nil; 
    } 
    CFRelease(addressBook); 

    } 
} 
+0

謝謝你的解決方案,但我上面選擇瞭解決方案。無論如何,再次感謝。 – obaid

+0

什麼是人的數據類型 – Vishnu

+0

人是NSObject類。 – jbchitaliya

相關問題