/// iOS 9前的框架 #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> /// iOS 9的新框架 #import <ContactsUI/ContactsUI.h> #define Is_up_Ios_9 ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) @interface UIViewController ()<ABPeoplePickerNavigationControllerDelegate,CNContactPickerDelegate> { CNContactPickerViewController * _peoplePickVC; } @property (nonatomic, strong) UIAlertController *alertController; @end - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ///获取通讯录权限,调用系统通讯录 [self CheckAddressBookAuthorization:^(bool isAuthorized , bool isUp_ios_9) { if (isAuthorized) { [self callAddressBook:isUp_ios_9]; }else { NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置"); [self showAlertViewAboutNotAuthorAccessContact]; } }]; } - (void)CheckAddressBookAuthorization:(void (^)(bool isAuthorized , bool isUp_ios_9))block { if (Is_up_Ios_9){ CNContactStore * contactStore = [[CNContactStore alloc]init]; if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) { [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * __nullable error) { if (error) { NSLog(@"Error: %@", error); } else if (!granted) { block(NO,YES); } else { block(YES,YES); } }]; } else if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized){ block(YES,YES); } else { NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置"); [self showAlertViewAboutNotAuthorAccessContact]; } }else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus(); if (authStatus == kABAuthorizationStatusNotDetermined) { ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { dispatch_async(dispatch_get_main_queue(), ^{ if (error) { NSLog(@"Error: %@", (__bridge NSError *)error); }else if (!granted){ block(NO,NO); } else { block(YES,NO); } }); }); } else if (authStatus == kABAuthorizationStatusAuthorized) { block(YES,NO); }else { NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置"); [self showAlertViewAboutNotAuthorAccessContact]; } } #pragma clang diagnostic pop } - (void)callAddressBook:(BOOL)isUp_ios_9 { if (isUp_ios_9) { CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init]; contactPicker.delegate = self; contactPicker.displayedPropertyKeys = @[CNContactPhoneNumbersKey]; [self presentViewController:contactPicker animated:YES completion:nil]; }else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init]; peoplePicker.peoplePickerDelegate = self; [self presentViewController:peoplePicker animated:YES completion:nil]; #pragma clang diagnostic pop } } #pragma mark - 用户点进去获取属性调用方法 例如从通讯录选择联系人打电话两个方法都写只调用上面方法 // 获取指定电话 - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty { CNPhoneNumber *phoneNumber = (CNPhoneNumber *)contactProperty.value; @weakify(self) [self dismissViewControllerAnimated:YES completion:^{ @strongify(self) /// 联系人 NSString *text1 = [NSString stringWithFormat:@"%@%@",contactProperty.contact.familyName,contactProperty.contact.givenName]; /// 电话 NSString *text2 = phoneNumber.stringValue; text2 = [text2 stringByReplacingOccurrencesOfString:@"+86" withString:@""]; text2 = [text2 stringByReplacingOccurrencesOfString:@"-" withString:@""]; text2 = [text2 stringByReplacingOccurrencesOfString:@"(" withString:@""]; text2 = [text2 stringByReplacingOccurrencesOfString:@")" withString:@""]; text2 = [text2 stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"联系人:%@, 电话:%@",text1,text2); }]; } #pragma mark -- ABPeoplePickerNavigationControllerDelegate #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty); CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier); CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index); CFStringRef anFullName = ABRecordCopyCompositeName(person); #pragma clang diagnostic pop [self dismissViewControllerAnimated:YES completion:^{ /// 联系人 NSString *text1 = [NSString stringWithFormat:@"%@",anFullName]; /// 电话 NSString *text2 = (__bridge NSString*)value; text2 = [text2 stringByReplacingOccurrencesOfString:@"+86" withString:@""]; text2 = [text2 stringByReplacingOccurrencesOfString:@"-" withString:@""]; text2 = [text2 stringByReplacingOccurrencesOfString:@"(" withString:@""]; text2 = [text2 stringByReplacingOccurrencesOfString:@")" withString:@""]; text2 = [text2 stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"联系人:%@, 电话:%@",text1,text2); }]; } - (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil]; } //提示没有通讯录权限 - (void)showAlertViewAboutNotAuthorAccessContact{ self.alertController = [UIAlertController alertControllerWithTitle:@"请授权通讯录权限" message:@"请在iPhone的\"设置-隐私-通讯录\"选项中,允许趣商旅访问你的通讯录" preferredStyle:UIAlertControllerStyleAlert]; [self.alertController addAction:([UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }])]; [self.alertController addAction:([UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //跳转 去设置权限 NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if([[UIApplication sharedApplication] canOpenURL:url]) { NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) { }]; } }])]; [self presentViewController:self.alertController animated:YES completion:nil]; }
相关推荐
在本例中,如果我们希望从其他页面跳转到通讯录页面,可以使用`pushViewController:animated:`方法将通讯录页面的ViewController推入导航控制器的堆栈。 总之,实现一个类似iPhone通讯录的UITableView,需要综合...
4. **返回充值界面**:当用户选择完联系人并获取到所需的电话号码后,可以通过`dismiss(animated:completion:)`方法关闭通讯录界面,并将选中的联系人信息传回充值界面,进行后续的充值操作。 5. **文件名称列表中...
该插件使用操作系统的本机UI来选择联系人,并且不需要用户的任何特殊权限。 当前,该插件仅支持选择电话号码。 但是,扩展插件以从联系人请求其他属性(例如地址)或获取联系人的完整记录(欢迎PR)应该很容易。 ...
IOS 由苹果公司开发的移动操作系统 Webkit 一个开源的浏览器引擎,在手机上的应用十分广泛 Webview WebView(网络视图)能加载显示网页,可以将其视为一个浏览器。它使用了WebKit渲染引擎加载显示网页 Activity ...
请注意,为了保持与iOS系统的兼容性,确保所使用的铃声文件格式为.m4r,这是苹果官方支持的铃声音频格式。同时,每个iPhone用户可自定义的铃声数量有限,具体数量取决于您的设备型号和iOS版本。 通过了解和使用这些...
在这个类中,你可以使用UITextField让用户输入名片信息,如姓名、电话等,再用UILabel显示这些信息的标题。添加一个UIButton,当用户点击时,将输入的信息保存到数据库中。 Delete类用于删除名片。这个页面会在用户...
在IT领域,通讯录列表是常见的一种数据展示方式,尤其在移动应用和桌面软件中,它使得用户能够方便地管理和查找联系人信息。本话题主要关注如何实现一个带有模糊搜索功能的通讯录列表,以及利用索引来提升用户体验。...
本节概述了Android操作系统的历史及其市场地位,强调了它在全球智能手机市场的主导地位。Android作为开源平台,为开发者提供了丰富的API和工具,使得移动应用开发变得更加便捷。 - **1.2 本书目的及涵盖范例范围**...