地址簿UI框架已弃用的方法

Sat*_*Sat 6 objective-c ios addressbookui

我正在研究一个现有的目标c项目,在阅读iOS的地址簿UI框架参考时,发现以下类已在iOS 9中弃用.(ABUnknownPersonViewController,ABPersonViewController,ABPeoplePickerNavigationController,ABNewPersonViewController)什么是更换了这一点.?在哪里我可以找到一些相关的文件.任何帮助赞赏.提前致谢 .

Soh*_*mon 11

AddressBookUI框架已被弃用iOS 9,因此您应该更好地使用ContactsUIFramework.

它有许多新功能,包括AddressBookUI框架的所有功能.

所以,如果你的目标是iOS 9具体的那么你应该去ContactsUI框架.

要检查该AddressBookUI框架是否可用于特定iOS版本,您可以执行以下操作:

if ([CNContactStore class]) {
  CNContactStore *store = [CNContactStore new];
  //...
} else {
  // Fallback to old framework
}
Run Code Online (Sandbox Code Playgroud)

这是完整的代码:

- (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 =@[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"];
    NSStrubg * 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;
}
Run Code Online (Sandbox Code Playgroud)

只需确保您要求许可从设备中读取联系人.

参考链接:https://gist.github.com/willthink/024f1394474e70904728

更新:

如需更换,则AddressBookUI需要使用CNContactPickerViewController.您可以检查可用于一次拾取一个或多个联系人的委托方法.

这将提供内置UIViewController所有联系人,你需要实现它的委托方法!

要选择一个联系人:

contactPicker:didSelectContact:
Run Code Online (Sandbox Code Playgroud)

要选择多个(新功能):

contactPicker:didSelectContacts:
Run Code Online (Sandbox Code Playgroud)

CNContactPickerDelegate参考:https://developer.apple.com/library/ios/documentation/ContactsUI/Reference/CNContactPickerDelegate_Protocol/