2013-08-16 1 views
0

저는 (iOS6을 사용하는) iphone 앱 개발을 처음 접했고 연락처 목록에서 UITableViewController로 휴대폰 번호를 가져 오는 데 어려움을 겪고 있습니다. 이름과 성을 올바르게 입력 할 수 있지만 전화 번호는 null로 반환됩니다. 이것 뒤에있는 이유를 이해할 수 없었다. 내가 뭘 잘못하고있는 걸까? 여기연락처 목록에서 휴대폰 번호를 가져올 수 없습니다. 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

이 숫자는 전화가 ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));

처럼 취해야 할 전체 작업 코드

-(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(), ^{ 



     }); 

} 

입니다 다음과 같이 내 코드는