iOS系统打开手机通讯录

苹果在iOS10更改了通讯的访问,之前的访问在没有开启权限的时候可能会报错,新版的使用如下



#define Is_up_Ios_9 [[UIDevice currentDevice].systemVersion floatValue] >= 9.0

// 之前的框架

#import "ContactsViewController.h"

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI.h>

/// iOS 9的新框架

#import <ContactsUI/ContactsUI.h>

@interface ContactsViewController ()<ABPeoplePickerNavigationControllerDelegate,CNContactPickerDelegate>

// 用于显示选中联系人的号码

@property (strong, nonatomic) IBOutlet UILabel *label;


@end


@implementation ContactsViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    


    

}


- (IBAction)touch:(id)sender {//点击事件,打开通讯录

    

    ///获取通讯录权限,调用系统通讯录

    [self CheckAddressBookAuthorization:^(bool isAuthorized , bool isUp_ios_9) {

        if (isAuthorized) {

            [self callAddressBook:isUp_ios_9];

        }else {

            

            NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置");

        }

    }];

}




- (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(@"请到设置>隐私>通讯录打开本应用的权限设置");

        }

    }else {

        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(@"请到设置>隐私>通讯录打开本应用的权限设置");

        }

    }

}

- (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 {

        ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];

        peoplePicker.peoplePickerDelegate = self;

        [self presentViewController:peoplePicker animated:YES completion:nil];

        

    }

}


#pragma mark -- CNContactPickerDelegate

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {

    CNPhoneNumber *phoneNumber = (CNPhoneNumber *)contactProperty.value;

    [self dismissViewControllerAnimated:YES completion:^{

        /// 联系人

        NSString *text1 = [NSString stringWithFormat:@"%@%@",contactProperty.contact.familyName,contactProperty.contact.givenName];

        /// 电话

        NSString *text2 = phoneNumber.stringValue;

        //        text2 = [text2 stringByReplacingOccurrencesOfString:@"-" withString:@""];

         _label.text = [NSString stringWithFormat:@"联系人:%@, 电话:%@",text1,text2];

        NSLog(@"联系人:%@, 电话:%@",text1,text2);

    }];

}


#pragma mark -- ABPeoplePickerNavigationControllerDelegate

- (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);

    

    [self dismissViewControllerAnimated:YES completion:^{

        /// 联系人

        NSString *text1 = [NSString stringWithFormat:@"%@",anFullName];

        /// 电话

        NSString *text2 = (__bridge NSString*)value;

                text2 = [text2 stringByReplacingOccurrencesOfString:@"-" withString:@""];

        _label.text = [NSString stringWithFormat:@"联系人:%@, 电话:%@",text1,text2];

        NSLog(@"联系人:%@, 电话:%@",text1,text2);

    }];

}






运行效果如下








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值