2016-09-04 2 views
1

나는 ABAddress 북으로 작업하기 시작했고 아주 간단한 출발점을 사용하고 있습니다 ... 내 주소록에있는 모든 항목을 가져 와서 배열에 넣고 싶습니다. 0 요소를 계속 표시합니다.ABAddressBookCopyArrayOfAllPeople 어떤 사람도 돌려 보내지 않습니다.

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); 
NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); 

ABAddressBookCopyArrayOfAllPeople은 실제로 연락처 목록에있는 모든 사람을 얻지 못하거나 내가 잘못 이해 했습니까?

참고로 ADMIN의 내 질문은 SWIFT와 관련된 Objective C와 관련되어 있으므로이 답변이 적용되지 않습니다. 추천 답변에서 Objective C에서 Swift 로의 OP 코드 마이그레이션이 왜 잘못되어 작동하지 않는지 설명합니다. 제 질문은 그렇지 않습니다. 나는 왜 내가 연락처 수백이 allContacts 항목을 가지고 내 아이폰에 이것을 설치하면 이해하려고 노력하고 있어요. 모든 연락처를 배열에 넣는 올바른 방법을 이해하려고 노력했습니다. 기본적으로 비즈니스 요구 때문에 나는 Apple의 내장을 사용하고 싶지 않기 때문에 자신의 연락처 선택 도구를 사용하고 있습니다.

+0

거짓말 쟁이가 아닙니다. 내 질문 다시보기 – logixologist

답변

1

먼저 사용 중단 된 api를 사용하려고합니다. 이는 ABAddressBookCreateWithOptions이 작동하지 않는 이유 일 수 있습니다.

그러나 내 권장 사항은 사용자의 UI를 작성하려는 편집자가 Contacts framework을 사용하는 것입니다.

this github gist를 참조하십시오

#import <Contacts/Contacts.h> 

@implementation ContactsScan 

- (void) contactScan 
{ 
    if ([CNContactStore class]) { 
     //ios9 or later 
     CNEntityType entityType = CNEntityTypeContacts; 
     if([CNContactStore authorizationStatusForEntityType:entityType] == CNAuthorizationStatusNotDetermined) 
     { 
      CNContactStore * contactStore = [[CNContactStore alloc] init]; 
      [contactStore requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError * _Nullable error) { 
       if(granted){ 
        [self getAllContact]; 
       } 
      }]; 
     } 
     else if([CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized) 
     { 
      [self getAllContact]; 
     } 
    } 
} 

-(void)getAllContact 
{ 
    if([CNContactStore class]) 
    { 
     //iOS 9 or later 
     NSError* contactError; 
     CNContactStore* addressBook = [[CNContactStore alloc]init]; 
     [addressBook containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers: @[addressBook.defaultContainerIdentifier]] error:&contactError]; 
     NSArray * keysToFetch [email protected][CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey]; 
     CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch]; 
     BOOL success = [addressBook enumerateContactsWithFetchRequest:request error:&contactError usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop){ 
      [self parseContactWithContact:contact]; 
     }]; 
    } 
} 

- (void)parseContactWithContact :(CNContact*)contact 
{ 
    NSString * firstName = contact.givenName; 
    NSString * lastName = contact.familyName; 
    NSString * phone = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"]; 
    NSString * email = [contact.emailAddresses valueForKey:@"value"]; 
    NSArray * addrArr = [self parseAddressWithContac:contact]; 
} 

- (NSMutableArray *)parseAddressWithContac: (CNContact *)contact 
{ 
    NSMutableArray * addrArr = [[NSMutableArray alloc]init]; 
    CNPostalAddressFormatter * formatter = [[CNPostalAddressFormatter alloc]init]; 
    NSArray * addresses = (NSArray*)[contact.postalAddresses valueForKey:@"value"]; 
    if (addresses.count > 0) { 
     for (CNPostalAddress* address in addresses) { 
      [addrArr addObject:[formatter stringFromPostalAddress:address]]; 
     } 
    } 
    return addrArr; 
} 

@end 

참고 :이 코드는 나에 의해 작성되지 않았습니다. 출처는 github에 있습니다.

+0

고마워요! 나는 이것에 대한 문서를 보았고 9.0에서 더 이상 사용되지 않는다고 말했다. 나는 그것이 일해야한다고 생각했을 정도로 9의 아래의 나의 것이 있었다. – logixologist

관련 문제