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 ...
随机推荐
- error C2065: CoInitializeEx' : undeclared identifier 解决方法
错误: error C2065: CoInitializeEx' : undeclared identifier 解决方法 原因: 本来程序的编译选项选择的是:使用标准windows库,当改为在静态库 ...
- 实现Comparator接口和Comparable接口,以及Map按value排序 ,map遍历
继承Comparator接口,重写compare()方法 import java.util.ArrayList; import java.util.Arrays; import java.util.C ...
- 2、获取APP CPU占用率
前面已经介绍过如何获取包名和主活动名.这里不再过多赘述.我们依旧采取两种方案实现APP CPU占有率 Windows下获取APP CPU占用率 adb shell "dumpsys cpui ...
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决
这是我们开启了bin-log, 我们就必须指定我们的函数是否是1 DETERMINISTIC 不确定的2 NO SQL 没有SQl语句,当然也不会修改数据3 READS SQL DATA 只是读取数据 ...
- java连连看小项目
/* *本人也是刚入门,希望各位多多指教 *该项目主要代码在于连线 *1.2个连线没有拐弯 *2.2个连线有一个拐弯 *3.2个连线有2个拐弯 *采用递归算法 */ package llk; impo ...
- springboot上传图片大小限制
背景:springboot项目上传图片超过1M报错,经了解,springboot默认上传文件1M 需求:更改默认配置,控制上传文件大小 方法:①更改配置文件(经试验不可行,不知道为什么):②更改启动B ...
- python--常用模块:collections 、time、random
一.collections 模块 1:nametuple 生成可以用名字访问内容的元祖 from collections import namedtuple point=namedtuple('p ...
- keepAlived主备及双主
nginx用默认配置即可 1.主备配置 1.主keepAlived配置 vrrp_instance VI_1 { state MASTER #主备区分 interface eth0 virtual_r ...
- linux 6 timezone修改
linux 6 / Amazon linux 因为正好在使用Amazon 的linux AMI 又遇到了需要修改系统时区这个case 所以就调查了一下修改方法,因为Amazon的linux版本是由A ...
- 试探回溯法(backtracking)
一.八皇后问题 国际象棋中皇后的势力范围覆盖其所在的水平线.垂直线以及两条对角线.现考查如下问题:在n*n的棋盘上放置n个皇后,如何使得她们彼此互不攻击,此时称她们构成一个可行的棋局.对于任何整数n ...