@iOS调用操作通讯录所用的库文件

                                        AddressBook.framework

                                        AddressBookUI.framework

#import "HMTMainViewController.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h> @interface HMTMainViewController ()<ABPeoplePickerNavigationControllerDelegate> @property (nonatomic,strong) ABPeoplePickerNavigationController *personPickVC;
@property (nonatomic,strong) UIWebView *phoneCallWebView; @end @implementation HMTMainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"系统通讯录"; // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(didClickDeletePersonAction)]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 70, 120, 40);
[button setTitle:@"选择联系人" forState:UIControlStateNormal];
[button addTarget:self action:@selector(didClickSelectAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button]; } - (void)didClickSelectAction{ // 选择电话薄的联系人
self.personPickVC = [[ABPeoplePickerNavigationController alloc] init];
_personPickVC.peoplePickerDelegate = self;
_personPickVC.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]];
[self deletePerson];
[self presentViewController:_personPickVC animated:YES completion:NULL];
//[self.navigationController pushViewController:[_personPickVC.viewControllers objectAtIndex:0] animated:YES];
//[self getAllPerson]; } // 进入选择联系人界面
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{ // 选中联系人后,就退出,并返回忆要的属性
[peoplePicker dismissViewControllerAnimated:YES completion:^{ NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSLog(@"person = %@",firstName); ABMultiValueRef phones = ABRecordCopyValue(person,kABPersonPhoneProperty);
for (int i = 0; i < ABMultiValueGetCount(phones); i++) {
NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
NSLog(@"telephone = %@",phone);
}
}]; 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;
} // 展示全部联系人
- (void)getAllPerson{ CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook);
for (id person in ((__bridge NSArray *)allPerson)) { NSString *firstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonFirstNameProperty);
NSLog(@"firstName = %@",firstName); // 由于一个用户可能有多个电话,所以须要循环调取
ABMultiValueRef phones = ABRecordCopyValue((__bridge ABRecordRef)person,kABPersonPhoneProperty);
for (int i = 0; i < ABMultiValueGetCount(phones); i++) {
NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
NSLog(@"telephone = %@",phone);
} }
} // 加入联系人
- (void)addPerson{ ABAddressBookRef addressBook = self.personPickVC.addressBook;
ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person, kABPersonFirstNameProperty, @"胡", nil);
ABRecordSetValue(person, kABPersonLastNameProperty, @"明涛", nil);
ABMutableMultiValueRef mulRef = ABMultiValueCreateMutable(kABStringPropertyType);
for (int i = 0; i < 1; i++) {
ABMultiValueIdentifier mutableIdentifier;
ABMultiValueAddValueAndLabel(mulRef, @"18690231234", nil, &mutableIdentifier);
}
ABRecordSetValue(person, kABPersonPhoneProperty, mulRef, nil);
ABAddressBookAddRecord(addressBook, person, nil);
ABAddressBookSave(addressBook, nil); } // 删除联系人
- (void)deletePerson{ CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook);
for (id person in (__bridge NSArray *)allPerson) { NSString *firstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonFirstNameProperty);
if ([firstName isEqualToString:@"胡"]) {
ABAddressBookRemoveRecord(self.personPickVC.addressBook, (__bridge ABRecordRef)person, nil);
}
}
ABAddressBookSave(self.personPickVC.addressBook, nil);
} // 拨打电话
- (void)dialPhoneNumber:(NSString *)phoneNumber{ // 1.UIWebView载入电话
NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]];
self.phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
[self.phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]]; // 2.私有方法
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10086"]];
}

@有的时候,我们按上诉方法訪问通讯录无效,这是由于,iOS6之后加强了对通讯录的訪问,要求我们显示的申请去訪问通讯录,加上例如以下几句代码就OK了:

    CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
// Request authorization to Address Book
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add the contact
//[self _addContactToAddressBook];
} else {
// User denied access
// Display an alert telling user the contact could not be added
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
//[self _addContactToAddressBook];
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaG10MjAxMzA0MTI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

iOS开发之系统通讯录的更多相关文章

  1. iOS开发——高级技术&通讯录功能的实现

    通讯录功能的实现 iOS 提供了对通讯录操作的组建,其中一个是直接操作通讯录,另一个是调用通讯录的 UI 组建.实现方法如下: 添加AddressBook.framework到工程中. 代码实现: 1 ...

  2. iOS:ABPeoplePickerNavigationController系统通讯录使用

    昨天因项目需求要访问系统通讯录获取电话号码,于是乎从一无所知,开始倒腾,倒腾了一下午,总算了弄好了.写这边博客是为了记录一下,自己下一次弄的时候就别在出错了.同时,有和我一样的菜鸟能够避免走一下弯路. ...

  3. iOS开发——高级技术&通讯录服务

    通讯录服务 AddressBook iOS中带有一 个Contacts应用程序来管理联系人,但是有些时候我们希望自己的应用能够访问或者修改这些信息,这个时候就要用到 AddressBook.frame ...

  4. iOS开发——高级篇——通讯录

    一.简介 1.如何访问用户的通讯录1)在iOS9之前有2个框架可以访问用户的通讯录AddressBookUI.framework提供了联系人列表界面.联系人详情界面.添加联系人界面等一般用于选择联系人 ...

  5. iOS开发 调用系统相机和相册

    调用系统相机和相册 (iPad,iPhone)打开相机:(iPad,iPhone)//先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库 ...

  6. iOS开发 调用系统相机和相册 分类: ios技术 2015-03-30 15:52 65人阅读 评论(0) 收藏

     调用系统相机和相册 (iPad,iPhone) 打开相机:(iPad,iPhone) //先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为 ...

  7. ios开发-调用系统自带手势

    在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureR ...

  8. ios开发之--系统控件显示中文

    虽然一直知道X-code肯定提供有语言本地化的设置地方,但是一直也做个记录,有些时候的汉化,还是需要使用代码去控制,键盘的右下角.navagiton的return使用代码修改,调用系统相机时,也是出现 ...

  9. IOS开发-UIBarButtonItem系统自带图标总结

    1.这四个返回的是后面的单词. UIBarButtonSystemItemDone UIBarButtonSystemItemCancel UIBarButtonSystemItemEdit UIBa ...

随机推荐

  1. Centos7 安装 telnet 服务

    准备写一个 django-webtelnet(运维管理系统集成后管理网络设备),但是手边没有现成的网络设备资源可以测试,那就研究下 Centos7 下安装 telnet-server 吧. 安装 yu ...

  2. js实现点击按钮传值

    js实现点击按钮传值 page1源码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...

  3. RocktMq安装和简单使用以及报错收集

    文章目录 安装 使用 报错 总结: rocketmq内存设置 配置brockerip 启动方式 如果往机器上部署,最好再本地看看报错吗 关于防火墙 看总结去吧 安装 准备: jdk1.8 maven ...

  4. hexo的next主题博客中加入分类页面的js,实现多级目录,并且能够点击展开,隐藏下级目录~(不知道算不算深度优化~~~)

    个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 多级标题 在自己的xxxx.md文件中做如下修 ...

  5. PAT_A1023#Have Fun with Numbers

    Source: PAT A1023 Have Fun with Numbers (20 分) Description: Notice that the number 123456789 is a 9- ...

  6. 前端(十三)—— JavaScript高级:回调函数、闭包、循环绑定、面向对象、定时器

    回调函数.闭包.循环绑定.面向对象.定时器 一.函数高级 1.函数回调 // 回调函数 function callback(data) {} // 逻辑函数 function func(callbac ...

  7. linux 创建用户并限制其访问目录

    1.创建用户及访问目录 useradd test1 -d /usr/share/webapps/test -M 设置密码 passwd  test1 将访问目录权限全部赋予用户 chown -R te ...

  8. javascript基础入门之js中的数据类型与数据转换01

    javascript基础入门之js中的数据结构与数据转换01 js的组成(ECMAScript.BOM.DOM)        js中的打印语句:        数据类型        变量      ...

  9. 深度探索C++对象模型之第三章:数据语义学

    如下三个类: class X { }: class Y :public virtual X { }; class Z : public virtual X {}; class A :public Y, ...

  10. C:\Windows\System32\drivers\etc中的hosts文件

    这个文件是根据TCP/IP for Windows 的标准来工作的,它的作用是包含IP地址和Host name(主机名)的映射关系,是一个映射IP地址和Host name(主机名)的规定,规定要求每段 ...