2013-08-16 90 views
0

我是iphone應用程序開發(使用iOS6)的新手,並且一直面臨將聯繫人列表中的移動號碼獲取到UITableViewController中的問題。我可以正確地得到名字和姓氏,但電話號碼被返回爲空。我無法理解這背後的原因。我做錯了什麼?我的代碼如下:無法從聯繫人列表中獲取手機號碼。 iOS6

NSMutableArray *people = (__bridge_transfer NSMutableArray *) ABAddressBookCopyArrayOfAllPeople (addressBookRef); 

     NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue((__bridge ABRecordRef)([people objectAtIndex:indexPath.row]), kABPersonFirstNameProperty); 

    NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue((__bridge ABRecordRef)([people objectAtIndex:indexPath.row]), kABPersonLastNameProperty); 

    ABMultiValueRef phoneNumbers = ABRecordCopyValue((__bridge ABRecordRef)([people objectAtIndex:indexPath.row]),kABPersonPhoneProperty); 

if (([firstName isEqualToString:@""] || [firstName isEqualToString:@"(null)"] || firstName == nil) && 
    ([lastName isEqualToString:@""] || [lastName isEqualToString:@"(null)"] || lastName == nil)) 
{ 
    // do nothing 
} 
else 
{ 
    aName = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 

    if ([firstName isEqualToString:@""] || [firstName isEqualToString:@"(null)"] || firstName == nil) 
    { 
     aName = [NSString stringWithFormat:@"%@", lastName]; 
    } 

    if ([lastName isEqualToString:@""] || [lastName isEqualToString:@"(null)"] || lastName == nil) 
    { 
     aName = [NSString stringWithFormat:@"%@", firstName]; 
    } 

    //[self.tableItems addObject:aName]; 
    NSLog(@"%@ added",aName); 
} 


     //fetch multiple phone nos. and use only 0th 
id person = people[indexPath.row]; 
ABMultiValueRef multi = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty); 
NSString* phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, 0); 
NSLog(@"%@",phone); 
[cell.detailTextLabel setText:phone]; 

[cell.textLabel setText:aName]; 
return cell; 

回答

1

這裏,這是一個完整的工作代碼

-(void)GetAddressBook 
{ 
    Contacts = [[NSMutableArray alloc]init]; 

    if (ABAddressBookCreateWithOptions) { 

     @try { 

      ABAddressBookRef addressBook = ABAddressBookCreate(); 
      // NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 
      if (!addressBook) { 
       NSLog(@"opening address book"); 
      } 
      CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
      CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 

      NSLog(@"opening address book ==%ld",nPeople); 

      for (int i=0;i < nPeople;i++) { 

       NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary]; 
       ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i); 
       NSString *Contact; 
       ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty)); 
       CFStringRef firstName, lastName; 
       NSMutableArray *array = [[NSMutableArray alloc]init]; 
       NSString *email; 
       firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty); 
       lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty); 
       ABMultiValueRef multiValueRef = ABRecordCopyValue(ref, kABPersonEmailProperty); 
       array = [(__bridge NSMutableArray *)ABMultiValueCopyArrayOfAllValues(multiValueRef) mutableCopy]; 
       email = ([array count] > 0) ? array[0] : @""; 

       if(firstName) 
       { 
        Contact = [NSString stringWithFormat:@"%@", firstName]; 
        if(lastName) 
         Contact = [NSString stringWithFormat:@"%@ %@",firstName,lastName]; 
       } 
       [dOfPerson setObject:Contact forKey:@"name"]; 
       [dOfPerson setObject:[NSString stringWithFormat:@"%d", i] forKey:@"id"]; 
       [dOfPerson setObject:[NSString stringWithFormat:@"%@",@""] forKey:@"found"]; 
       [dOfPerson setObject:email forKey:@"email"]; 

       NSString* mobileLabel; 
       for(CFIndex j = 0; j< ABMultiValueGetCount(phones); j++) 
       { 
        mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, j); 
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) 
        { 
         [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"]; 
        } 
        else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) 
        { 
         [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"]; 
         break ; 
        } 
       } 
       [Contacts addObject:dOfPerson]; 
      } 
     } 
     @catch (NSException * e) { 
      NSLog(@"Exception: %@", e); 


     } 
     dispatch_async(dispatch_get_main_queue(), ^{ 



     }); 

} 

的電話號碼是要採取像ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));

相關問題