我正在研究一个现有的目标c项目,在阅读iOS的地址簿UI框架参考时,发现以下类已在iOS 9中弃用.
(ABUnknownPersonViewController,ABPersonViewController,ABPeoplePickerNavigationController,ABNewPersonViewController)
有什么可以替代它.在哪里我可以找到一些相关的文件.任何帮助赞赏.提前致谢 .
(ABUnknownPersonViewController,ABPersonViewController,ABPeoplePickerNavigationController,ABNewPersonViewController)
有什么可以替代它.在哪里我可以找到一些相关的文件.任何帮助赞赏.提前致谢 .
解决方法
AddressBookUI框架已在iOS 9中弃用,因此最好使用ContactsUI Framework.
它有许多新功能,包括AddressBookUI框架的所有功能.
因此,如果您专门针对iOS 9,那么您应该选择ContactsUI Framework.
要检查AddressBookUI框架是否适用于特定的iOS版本,您可以执行以下操作:
if ([CNContactStore class]) { CNContactStore *store = [CNContactStore new]; //... } else { // Fallback to old framework }
这是完整的代码:
- (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; }
只需确保您要求许可从设备中读取联系人.
参考链接:https://gist.github.com/willthink/024f1394474e70904728
Updated:
要替换AddressBookUI,您需要使用CNContactPickerViewController.您可以检查可用于一次拾取一个或多个联系人的委托方法.
这将呈现一个包含所有联系人的内置UIViewController,您需要实现它的委托方法!
To select one contact:
contactPicker:didSelectContact:
To select multiple (New Feature):
contactPicker:didSelectContacts:
CNContactPickerDelegate参考:https://developer.apple.com/library/ios/documentation/ContactsUI/Reference/CNContactPickerDelegate_Protocol/