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

                                        AddressBook.framework

                                        AddressBookUI.framework

  1. #import "HMTMainViewController.h"
  2. #import <AddressBook/AddressBook.h>
  3. #import <AddressBookUI/AddressBookUI.h>
  4. @interface HMTMainViewController ()<ABPeoplePickerNavigationControllerDelegate>
  5. @property (nonatomic,strong) ABPeoplePickerNavigationController *personPickVC;
  6. @property (nonatomic,strong) UIWebView *phoneCallWebView;
  7. @end
  8. @implementation HMTMainViewController
  9. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  10. {
  11. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  12. if (self) {
  13. // Custom initialization
  14. }
  15. return self;
  16. }
  17. - (void)viewDidLoad
  18. {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. self.navigationItem.title = @"系统通讯录";
  22. // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(didClickDeletePersonAction)];
  23. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  24. button.frame = CGRectMake(100, 70, 120, 40);
  25. [button setTitle:@"选择联系人" forState:UIControlStateNormal];
  26. [button addTarget:self action:@selector(didClickSelectAction) forControlEvents:UIControlEventTouchUpInside];
  27. [self.view addSubview:button];
  28. }
  29. - (void)didClickSelectAction{
  30. // 选择电话薄的联系人
  31. self.personPickVC = [[ABPeoplePickerNavigationController alloc] init];
  32. _personPickVC.peoplePickerDelegate = self;
  33. _personPickVC.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]];
  34. [self deletePerson];
  35. [self presentViewController:_personPickVC animated:YES completion:NULL];
  36. //[self.navigationController pushViewController:[_personPickVC.viewControllers objectAtIndex:0] animated:YES];
  37. //[self getAllPerson];
  38. }
  39. // 进入选择联系人界面
  40. - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
  41. // 选中联系人后,就退出,并返回忆要的属性
  42. [peoplePicker dismissViewControllerAnimated:YES completion:^{
  43. NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
  44. NSLog(@"person = %@",firstName);
  45. ABMultiValueRef phones = ABRecordCopyValue(person,kABPersonPhoneProperty);
  46. for (int i = 0; i < ABMultiValueGetCount(phones); i++) {
  47. NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
  48. NSLog(@"telephone = %@",phone);
  49. }
  50. }];
  51. return YES;
  52. }
  53. - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
  54. [peoplePicker dismissViewControllerAnimated:YES completion:^{
  55. }];
  56. }
  57. - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
  58. return YES;
  59. }
  60. // 展示全部联系人
  61. - (void)getAllPerson{
  62. CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook);
  63. for (id person in ((__bridge NSArray *)allPerson)) {
  64. NSString *firstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonFirstNameProperty);
  65. NSLog(@"firstName = %@",firstName);
  66. // 由于一个用户可能有多个电话,所以须要循环调取
  67. ABMultiValueRef phones = ABRecordCopyValue((__bridge ABRecordRef)person,kABPersonPhoneProperty);
  68. for (int i = 0; i < ABMultiValueGetCount(phones); i++) {
  69. NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
  70. NSLog(@"telephone = %@",phone);
  71. }
  72. }
  73. }
  74. // 加入联系人
  75. - (void)addPerson{
  76. ABAddressBookRef addressBook = self.personPickVC.addressBook;
  77. ABRecordRef person = ABPersonCreate();
  78. ABRecordSetValue(person, kABPersonFirstNameProperty, @"胡", nil);
  79. ABRecordSetValue(person, kABPersonLastNameProperty, @"明涛", nil);
  80. ABMutableMultiValueRef mulRef = ABMultiValueCreateMutable(kABStringPropertyType);
  81. for (int i = 0; i < 1; i++) {
  82. ABMultiValueIdentifier mutableIdentifier;
  83. ABMultiValueAddValueAndLabel(mulRef, @"18690231234", nil, &mutableIdentifier);
  84. }
  85. ABRecordSetValue(person, kABPersonPhoneProperty, mulRef, nil);
  86. ABAddressBookAddRecord(addressBook, person, nil);
  87. ABAddressBookSave(addressBook, nil);
  88. }
  89. // 删除联系人
  90. - (void)deletePerson{
  91. CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook);
  92. for (id person in (__bridge NSArray *)allPerson) {
  93. NSString *firstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonFirstNameProperty);
  94. if ([firstName isEqualToString:@"胡"]) {
  95. ABAddressBookRemoveRecord(self.personPickVC.addressBook, (__bridge ABRecordRef)person, nil);
  96. }
  97. }
  98. ABAddressBookSave(self.personPickVC.addressBook, nil);
  99. }
  100. // 拨打电话
  101. - (void)dialPhoneNumber:(NSString *)phoneNumber{
  102. // 1.UIWebView载入电话
  103. NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]];
  104. self.phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
  105. [self.phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
  106. // 2.私有方法
  107. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10086"]];
  108. }

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

  1. CFErrorRef *error = nil;
  2. ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
  3. // Request authorization to Address Book
  4. if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
  5. ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
  6. if (granted) {
  7. // First time access has been granted, add the contact
  8. //[self _addContactToAddressBook];
  9. } else {
  10. // User denied access
  11. // Display an alert telling user the contact could not be added
  12. }
  13. });
  14. }
  15. else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
  16. // The user has previously given access, add the contact
  17. //[self _addContactToAddressBook];
  18. }
  19. else {
  20. // The user has previously denied access
  21. // Send an alert telling user to change privacy setting in settings app
  22. }

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. error LNK2001: 无法解析的外部符号 __imp__MessageBoxA@16

    错误: error LNK2001: 无法解析的外部符号 __imp__MessageBoxA@16 原因: 本来程序的编译选项选择的是:使用标准windows库,当改为在静态库中使用MFC后就出现了 ...

  2. elasticsearch中TermQuery查不到数据问题

    在java rest client中调用elasticsearch中的数据,精准匹配的termQuery查不到数据,这个问题是java rest client客户端自带的bug,换用matchPhra ...

  3. 拾遗:关于“尾递归”- tail recursion

    定义[个人理解]: 尾递归,即是将外层得出的常量计算因子,以函数参数的形式逐层向内传递,即内层调用整合外层调用的产出,整个递归的结果最终由最内层的一次函数调用得出:而通常的递归则是外层调用阻塞.等待内 ...

  4. java8如何对List<Bean>进行去重和覆盖

    背景:有一批数据源从kafka给过来,接收到后需要处理,然后入库,我们用一个线程消费下来,一次消费30000条, 按照对象的概念,可以用List<Person>来表示,因为某种原因,需要根 ...

  5. JDK8新特性之方法引用

    什么是方法引用 方法引用是只需要使用方法的名字,而具体调用交给函数式接口,需要和Lambda表达式配合使用. 如: List<String> list = Arrays.asList(&q ...

  6. 在IDEA中用Gradle构建项目时使用lombok以依赖出现出错

    情景: 之情一直是使用Maven构建的项目并且导入依赖后都可以正常使用,但是在换成Gradle时出现了不论使用什么版本的lombok的依赖都会提示@Sl4j注解的log找不到,但是编辑界面是不会报错的 ...

  7. Linux网络编程 了解

    IPV4 -- IP地址分类:主机号是区分主机的,网络号是区分网段的 子网掩码是对主机号进行划分子网用的 举例说明: 对 192.168.1.0网段划分4个 其子网掩码 : 拿出主机号的两个位进行划分 ...

  8. redis 分析rdb中key

    1.问题: 单位一个redis集群内存报警,想找出所有的key的列表? 2.解决办法: 网上搜索是可以用redis-rdb-tools 这个工具进行分析 (1)centos6 默认安装python2. ...

  9. python 2 学习历程(一)

    在用户输入字符串的时候,有时会带有一些其他的字符,例如常见的空格 除非在网页或者某个位置声明了空格也算字符,或者一些账号等安全程度较高的环节,多了一个空格很少有人会注意到,并且愿意即时改正它们,那么这 ...

  10. 【模板篇】Link Cut Tree模板(指针)

    网上一片一片的LCT都是数组写的 orz 用指针写splay的人想用指针写LCT找板子都不好找QAQ 所以能A题了之后自然要来回报社会, 把自己的板子丢上来(然而根本没有人会看) LCT讲解就省省吧, ...