iOS开发之系统通讯录
@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开发之系统通讯录的更多相关文章
- iOS开发——高级技术&通讯录功能的实现
通讯录功能的实现 iOS 提供了对通讯录操作的组建,其中一个是直接操作通讯录,另一个是调用通讯录的 UI 组建.实现方法如下: 添加AddressBook.framework到工程中. 代码实现: 1 ...
- iOS:ABPeoplePickerNavigationController系统通讯录使用
昨天因项目需求要访问系统通讯录获取电话号码,于是乎从一无所知,开始倒腾,倒腾了一下午,总算了弄好了.写这边博客是为了记录一下,自己下一次弄的时候就别在出错了.同时,有和我一样的菜鸟能够避免走一下弯路. ...
- iOS开发——高级技术&通讯录服务
通讯录服务 AddressBook iOS中带有一 个Contacts应用程序来管理联系人,但是有些时候我们希望自己的应用能够访问或者修改这些信息,这个时候就要用到 AddressBook.frame ...
- iOS开发——高级篇——通讯录
一.简介 1.如何访问用户的通讯录1)在iOS9之前有2个框架可以访问用户的通讯录AddressBookUI.framework提供了联系人列表界面.联系人详情界面.添加联系人界面等一般用于选择联系人 ...
- iOS开发 调用系统相机和相册
调用系统相机和相册 (iPad,iPhone)打开相机:(iPad,iPhone)//先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库 ...
- iOS开发 调用系统相机和相册 分类: ios技术 2015-03-30 15:52 65人阅读 评论(0) 收藏
调用系统相机和相册 (iPad,iPhone) 打开相机:(iPad,iPhone) //先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为 ...
- ios开发-调用系统自带手势
在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureR ...
- ios开发之--系统控件显示中文
虽然一直知道X-code肯定提供有语言本地化的设置地方,但是一直也做个记录,有些时候的汉化,还是需要使用代码去控制,键盘的右下角.navagiton的return使用代码修改,调用系统相机时,也是出现 ...
- IOS开发-UIBarButtonItem系统自带图标总结
1.这四个返回的是后面的单词. UIBarButtonSystemItemDone UIBarButtonSystemItemCancel UIBarButtonSystemItemEdit UIBa ...
随机推荐
- springboot启动失败( No active profile set, falling back to default profiles: default)
问题: springboot启动失败( No active profile set, falling back to default profiles: default) 解决方法 在pom.xml文 ...
- 【转】java使用java.lang.management监视和管理 Java 虚拟机
原文地址:https://blog.csdn.net/zhongweijian/article/details/7619383 软件包 java.lang.management 提供管理接口,用于监视 ...
- jQuery 事件委派
/******************************************************************/ $(function(){ //live()事件委派,后续添加 ...
- SPOJ MAXMATCH - Maximum Self-Matching (FFT)
题目链接:MAXMATCH - Maximum Self-Matching Description You're given a string s consisting of letters 'a', ...
- zabbix--添加用户
用户和用户组 概述 Zabbix 中的所有用户都通过 Web 前端去访问 Zabbix 应用程序.并为每个用户分配唯一的登陆名和密码. 所有用户的密码都被加密并储存于 Zabbix 数据库中.用户 ...
- JS 将对象转换成字符 字符串转换成json对象
//js对象 var user = { "name": "张学友", "address": "中国香港" }; //将对 ...
- AVR446步进电机算法推导及应用
https://blog.csdn.net/Renjiankun/article/details/80513839?utm_source=copy
- python--序列化:json、pickle、shelva
序列化 数据类型--->str 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 反序列化 str--->数据类型 序列化三大方法:json *** ...
- 结对编程项目报告--四则运算CORE
<!doctype html> sw_lab2.mdhtml {overflow-x: initial !important;}#write, body { height: auto; } ...
- cdn 的配置及原理
CDN概况 CDN的全称是Content Delivery Network,即内容分发网络. CND加速主要是加速静态资源,如网站上面上传的图片.媒体,以及引入的一些Js.css等文件. CND加速需 ...