`

iOS 从系统电话薄返回选中的用户电话信息

    博客分类:
  • ios
 
阅读更多
/// 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];
}



 

分享到:
评论

相关推荐

    ios-将系统返回手势拦截到自定义手势上(全屏返回).zip

    默认情况下,iOS中的UINavigationController提供了一个滑动手势,当用户从屏幕左侧边缘向右滑动时,这个手势会触发导航栈的pop操作,即返回上一个视图控制器。这个手势是由UINavigationController内部的一个...

    ios图书管理系统

    2. 删除图书:用户可以选中图书列表中的某条记录,点击删除按钮,系统会从数据模型中移除对应的Book实例,并更新界面显示。为了防止误操作,通常会有确认提示。 3. 修改图书:当用户选择编辑某一图书时,系统会弹出...

    ios6系统图标原生

    在iOS系统中,图标是用户界面的重要组成部分,它们为用户提供了一目了然的视觉指示,帮助用户快速识别和理解应用程序的功能。"ios6系统图标原生"是指iOS 6操作系统中,苹果公司官方设计的一系列应用程序图标。这些...

    ios点餐系统源码

    这个系统可能涵盖了从用户界面设计、数据管理到网络通信等多个关键环节,下面将详细探讨其中涉及的主要知识点。 一、Swift编程语言 作为苹果官方推荐的iOS应用开发语言,Swift是理解点餐系统源码的基础。Swift语法...

    iOS 点餐系统例子分享

    在本篇中,我们将深入探讨一个iOS点餐系统的实现,这是一个专门为移动设备设计的应用程序,让用户可以在手机上方便地浏览菜单、下单并完成支付。这个系统通常包括几个关键组件,如用户界面、数据库管理、订单处理和...

    iOS 解决侧滑返回各种不兼容问题

    在iOS开发中,侧滑返回(Swipe Back)是苹果提供的一种常见手势,让用户可以方便地在导航控制器的视图之间切换。然而,在某些特定场景下,如使用ScrollView或地图组件时,侧滑返回可能会与其他交互产生冲突,导致不...

    ios操作系统与安卓系统对比的优势

    根据市场调研数据,iOS应用商店App Store的用户通常愿意为高质量的应用付费,而Android平台上的Google Play虽然用户基数更大,但由于免费模式较为普遍,因此平均每位开发者从iOS平台获得的收入远高于Android平台。...

    仿iOS拨打电话提示界面

    在iOS系统中,拨打电话的交互设计以其简洁、直观而著名。为了在其他平台上实现类似的效果,我们可以创建一个仿iOS拨打电话提示界面。这个界面通常包括电话号码输入框、拨打按钮、取消按钮以及可能的联系人选择功能。...

    iOS 14系统版本测试真机包,有需要的可以下载

    通话界面也进行了优化,电话、FaceTime和其他通话会以一个小窗口的形式出现,而不是全屏覆盖,使得用户在通话期间仍能继续使用其他应用。 Siri在iOS 14中也有了新的设计,不再占据整个屏幕,而是以一个紧凑的气泡...

    iOS 管理系统

    在iOS开发领域,构建一个管理系统是一项复杂而重要的任务。这个名为“iOS 管理系统”的项目提供了一个全面的解决方案,特别适用于企业级应用,如公司管理系统和图书馆管理系统。通过这个参考Demo,开发者可以获得...

    IOS签名证书 苹果签名w系统免费工具

    IOS签名证书 苹果签名w系统免费工具IOS签名证书 苹果签名w系统免费工具IOS签名证书 苹果签名w系统免费工具IOS签名证书 苹果签名w系统免费工具IOS签名证书 苹果签名w系统免费工具IOS签名证书 苹果签名w系统免费工具...

    ios获取手机系统信息源码

    在iOS开发中,获取设备的系统信息是一项常见的需求,这对于调试、日志记录以及提供个性化用户体验都至关重要。本文将深入探讨如何使用Objective-C(OC)语言来获取iPhone或iPad的系统版本号、MAC地址等关键数据信息...

    iOS点菜系统 pickerView

    综上所述,构建一个iOS点菜系统涉及到了数据模型设计、UI组件使用、用户交互处理、性能优化等多个方面,而`UIPickerView`在此中扮演了展示和选择菜品分类或参数的重要角色。通过合理的设计和实现,可以打造一款高效...

    ios 7系统下载

    在深入探讨iOS 7系统下载的相关知识点之前,我们先对苹果公司于2013年推出的这一里程碑式操作系统——iOS 7做一个全面的回顾与解析。iOS 7不仅仅是苹果iOS系列的一个新版本,它标志着苹果设计哲学的一次重大转变,...

    ios系统机制介绍

    在数据安全性方面,iOS系统具有强大的防护机制,保护用户隐私不被侵犯,这在当前个人信息安全问题日益突出的时代尤为重要。 App Store作为iOS应用的主要来源,拥有数十万款应用,涵盖了生活、工作、娱乐等各个领域...

    iOS 监听系统侧滑返回事件.zip

    操作系统:LInux、IOS、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络...

    查看iOS设备系统字体

    在iOS开发中,了解系统内置的字体对于UI设计和用户体验优化至关重要。本文将深入探讨如何查看iOS设备系统中的字体,并展示这些字体在显示中英文时的效果。我们将主要围绕以下几点展开: 1. iOS系统字体概述 2. 获取...

    iOS原生文件系统解析.zip

    "iDevices RAW File System.doc" 提供了关于iOS原始文件系统的详细信息,可能涵盖如何直接访问和解析这些文件。在iOS中,由于安全和隐私的考虑,普通用户通常无法直接访问到这些原始文件,但开发者或高级用户可以...

    ios helper用于苹果ios系统的实用工具

    并且,当需要时,用户可以方便地从备份中恢复数据,确保信息完整无缺。 再者,应用管理和下载也是iOS Helper的重要组成部分。对于iOS用户来说,App Store是获取应用的主要途径,但有时可能会遇到下载限制或者地区...

Global site tag (gtag.js) - Google Analytics