Ios 通讯簿UI框架不推荐的方法
我正在处理一个现有的objective c项目,在阅读iOS的通讯簿UI框架参考时,我发现以下类在iOS 9中已被弃用。 (Ios 通讯簿UI框架不推荐的方法,ios,objective-c,addressbookui,Ios,Objective C,Addressbookui,我正在处理一个现有的objective c项目,在阅读iOS的通讯簿UI框架参考时,我发现以下类在iOS 9中已被弃用。 (ABUnknownPersonViewController,ABPersonViewController,ABPeoplePickerNavigationController,ABNewPersonViewController) 这个的替代品是什么。?在那里我可以找到一些与此相关的文档。谢谢你的帮助。提前感谢。苹果为iOS9及以上版本引入了新的框架,请点击下面的链接 编辑:
ABUnknownPersonViewController
,ABPersonViewController
,ABPeoplePickerNavigationController
,ABNewPersonViewController
)
这个的替代品是什么。?在那里我可以找到一些与此相关的文档。谢谢你的帮助。提前感谢。苹果为iOS9及以上版本引入了新的框架,请点击下面的链接 编辑:
还有一个链接:在
iOS 9
中,AddressBookUI
框架已被弃用,因此最好使用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;
}
只需确保您请求从设备读取联系人的权限即可
参考链接:
更新:
要替换AddressBookUI
,您需要使用CNContactPickerViewController
。您可以检查可用于一次拾取一个或多个联系人的委托方法
这将为所有联系人提供一个内置的UIViewController
,您需要实现它的委托方法
要选择一个联系人,请执行以下操作:
要选择多个(新功能):
CNContactPickerDelegate
reference:我假设ContactsUI将不可用于,但返回到旧方法仍将在我的项目中保留不推荐的警告。那么,搬家的动机是什么呢?不完全是。尽管已弃用,AddressBookUI
在我的应用程序中仍然适用于iOS 9。无论如何,谢谢你。我相信如果你指定的部署目标小于iOS 9.0,Xcode会停止警告你不推荐吗?发生在其他不推荐的方法/类等@Rob I已更新答案。谢谢你发现了我!请避免仅链接答案。至少您应该提到新框架的名称(在本例中为ContactsUI
),这样,如果将来链接中断,阅读答案的人至少可以按名称搜索它。
contactPicker:didSelectContact:
contactPicker:didSelectContacts: