ios 使用 通讯录 picker 快速 拾取 用户 手机号码 代码分享

ViewController.h


#import <AddressBookUI/AddressBookUI.h>

#import <AddressBook/AddressBook.h>

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>


@end


ViewController.m


@implementation ViewController

//弹出 通讯录 拭去 UI 的方法

-(void)showAddressBookBtnClicked:(UIButton*)sender{

     sender.enabled = NO;

    __block UIButton * bBtn = sender;

    ABPeoplePickerNavigationController * peoplePickerNav = [[ABPeoplePickerNavigationControlleralloc] init];

    NSArray *displayedItems =@[[NSNumbernumberWithInt:kABPersonPhoneProperty]]; // 只显示 电话号码

    peoplePickerNav.displayedProperties = displayedItems;

    peoplePickerNav.peoplePickerDelegate = self;

    [self.navigationControllerpresentViewController:peoplePickerNav animated:YEScompletion:^{

        bBtn.enabled = YES;

    }];

    

}

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

    [peoplePicker dismissViewControllerAnimated:YEScompletion:nil];

}

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

    return YES;

}

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

    

    ABPropertyType propertyType =ABPersonGetTypeOfProperty(property);

    if (propertyType ==kABMultiStringPropertyType) {

        CFTypeRef phoneProperty = ABRecordCopyValue(person, property);

        int index = ABMultiValueGetIndexForIdentifier(phoneProperty, identifier);

        CFTypeRef value =  ABMultiValueCopyValueAtIndex(phoneProperty,index);

        NSString * valueString = (__bridge_transferNSString*)value;

        NSString * pureNumbers = [[valueStringcomponentsSeparatedByCharactersInSet:[[NSCharacterSetcharacterSetWithCharactersInString:@"0123456789"]invertedSet]] componentsJoinedByString:@""]; // 将 字符串中可能的 非 号码 字符 剔除

        

NSLog(@"phoneNumber %@ ",pureNumbers);  //最终所得到的 电话号码

    }

    [peoplePicker dismissViewControllerAnimated:YEScompletion:nil]; 

    return NO;

    

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值