30.ios 8 访问通讯录兼容问题,ABPeoplePickerNavigationController

要访问系统的通讯录,首先需要添加 AddressBook.framework 和 AddressBookUI.framework 两个框架到你工程中build phase的"Link Binary With Libraries"之下,然后就可以开始了。

首先我们需要创建一个控制器:ViewController,在.h文件中导入头文件: <AddressBook/AddressBook.h>、  <AddressBookUI/AddressBookUI.h>,

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

然后在控制器实现 ABPeoplePickerNavigationControllerDelegate协议

@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>

-(void)chooseContacts{

    _personPickVC = [[ABPeoplePickerNavigationController alloc] init];

    _personPickVC.peoplePickerDelegate = self;

    _personPickVC.displayedProperties = @[[NSNumbernumberWithInt:kABPersonPhoneProperty]];

    [self presentViewController:_personPickVC animated:YEScompletion:NULL];

}

//适配IOS8

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0) {

    [peoplePicker dismissViewControllerAnimated:YES completion:^{

        

        ABMultiValueRef phones =ABRecordCopyValue(person,kABPersonPhoneProperty);

        long index = ABMultiValueGetIndexForIdentifier(phones,identifier);

        NSString *phoneNO = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, index);

        NSMutableString *mutableStr = [[NSMutableString alloc] init];

        NSCharacterSet *set = [NSCharacterSetcharacterSetWithCharactersInString:@" -"];

        NSArray *arr = [phoneNOcomponentsSeparatedByCharactersInSet:set];

        for (NSString *str in arr) {

            [mutableStr appendString:str];

        }

        _phoneNumtextField.text = mutableStr;

     [peoplePicker dismissViewControllerAnimated:YES completion:^{ }];

    

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    

    if (buttonIndex < actionSheet.numberOfButtons -1) {

        _phoneNumtextField.text = _phoneNumbers[buttonIndex];

    }

}

//  进入选择联系人界面

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{

    

    [peoplePicker dismissViewControllerAnimated:YES completion:^{

        

        ABMultiValueRef phones =ABRecordCopyValue(person,kABPersonPhoneProperty);

        //多个联系人号码处理

        [_phoneNumbers removeAllObjects];

        for (int i = 0; i < ABMultiValueGetCount(phones); i++) {

            NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));

            NSLog(@"号码:%@",phone);

            //因为号码中可能含有空格或者- 所以需要分割

            NSMutableString *mutableStr = [[NSMutableString alloc] init];

            NSCharacterSet *set = [NSCharacterSetcharacterSetWithCharactersInString:@" -"];

            NSArray *arr = [phonecomponentsSeparatedByCharactersInSet:set];

            for (NSString *str in arr) {

                [mutableStr appendString:str];

            }

            [_phoneNumbers addObject:mutableStr];

        }

        if ([_phoneNumbers count] >1) {

            

            UIActionSheet *phoneAS = [[UIActionSheet alloc] initWithTitle:nildelegate:self cancelButtonTitle:nil destructiveButtonTitle:nilotherButtonTitles: nil];

            for (NSString *phoneStr in _phoneNumbers) {

                [phoneAS addButtonWithTitle:phoneStr];

            }

            [phoneAS addButtonWithTitle:@"取消"];

            phoneAS.cancelButtonIndex = phoneAS.numberOfButtons -1;

            phoneAS.actionSheetStyle =UIActionSheetStyleDefault; //chenyy可支持三种颜色样式

            [phoneAS showInView:self.view];

        }else{

           _phoneNumtextField.text = _phoneNumbers[0];

        }

    }];

    return YES;

}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{

    

    [peoplePicker dismissViewControllerAnimated:YES completion:^{

        

    }];

}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{

    

    return YES;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值