我正在开发一个现有的目标c项目,在阅读iOS的地址簿UI框架参考时,发现以下类已在iOS 9中弃用。
(ABUnknownPersonViewController
,ABPersonViewController
,ABPeoplePickerNavigationController
,ABNewPersonViewController
)
有什么可以替代它。在哪里我可以找到一些相关的文件。任何帮助赞赏。提前谢谢。
答案 0 :(得分:11)
AddressBookUI
已弃用iOS 9
框架,因此最好使用ContactsUI
框架。
它有许多新功能,包括AddressBookUI
框架的所有功能。
因此,如果您专门定位iOS 9
,那么您应该选择ContactsUI
框架。
要检查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
更新:
要替换AddressBookUI
,您需要使用CNContactPickerViewController
。您可以检查可用于一次拾取一个或多个联系人的委托方法。
这将显示一个包含所有联系人的内置UIViewController
,您需要实现它的委托方法!
选择一个联系人:
contactPicker:didSelectContact:
选择多个(新功能):
contactPicker:didSelectContacts:
CNContactPickerDelegate
参考:https://developer.apple.com/library/ios/documentation/ContactsUI/Reference/CNContactPickerDelegate_Protocol/
答案 1 :(得分:0)