您的位置:首页 > 移动开发 > IOS开发

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

2014-09-11 12:34 471 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐