30.ios 8 访问通讯录兼容问题,ABPeoplePickerNavigationController
2015-09-01 17:21
387 查看
要访问系统的通讯录,首先需要添加
phase的"Link Binary With Libraries"之下,然后就可以开始了。
首先我们需要创建一个控制器:ViewController,在.h文件中导入头文件: <AddressBook/AddressBook.h>、 <AddressBookUI/AddressBookUI.h>,
然后在控制器实现 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;
}
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;
}
相关文章推荐
- 31.ios 防止按钮快速点击造成多次响应的避免方法。 取消正在执行中的方法
- 29. IOS系统原生富文本 自定义Label颜色
- IOS第17天(2,Quartz2D图片剪裁变圆行图,和截屏图片)
- 23.iOS学习笔记(十四)——发短信
- 24. iOS中文网址路径转换URLEncode
- 22.IOS开发中(null)与<null>的处理
- Ios 多国语言的使用(已汉语和英语为例)
- IOS第17天(1,Quartz2D图片水印)
- ios原生二维码扫描
- iOS CGAffineTransform详解
- 16.IOS 新消息通知提示-声音、震动
- IOS第16天(5,Quartz2D雪花)
- IOS第16天(4,Quartz2D柱状图)
- IOS第16天(3,Quartz2D饼图)
- IOS第16天(2,Quartz2D下载进度条)
- 14.IOS中系统唯一标识ID
- iOS KVC,KVO
- 4.极光推送解释的iOS本地通知
- 5.iOS 7 Background Remote Notification
- 2.iOS苹果推送-一些示例代码和注意的问题