2013-08-16 66 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));

相关问题