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 ...
随机推荐
- 解决Python报错:local variable 'xxx' referenced before assignment(引)
解决Python报错:local variable 'xxx' referenced before assignment(引) 解决Python报错:local variable 'xxx' refe ...
- Java传输对象模式
当我们想要在客户端到服务器的一个传递具有多个属性的数据时,可使用传输对象模式.传输对象也称为值对象.传输对象是一个具有getter/setter方法的简单POJO类,并且是可序列化的,因此可以通过网络 ...
- C#内嵌Python架构实现
C#通过IronPython内嵌Python脚本,实现了对业务逻辑抽象及判断,适合在大量订单需要进行校验的场合使用. 比如,贷款时会对用户进行核查,核查过程可能存在多个节点,并且节点可能会随着政策而不 ...
- [USACO10FEB]购买巧克力Chocolate Buying
题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...
- winform 使用webbrowser 打开不了pdf的解决办法
最近有个项目需要在winform 打开网络路径的pdf文件,自然想到了webbrowser,但是让我没想到的是,在我电脑调试一点问题都没有,但是到了其他同事的电脑是各种各样的问题,有的打不开,有的显示 ...
- 启动eclipse出现JVM terminated. Exit code=127 错误解决办法
https://blog.csdn.net/wpzsidis/article/details/72954387 进去第二次又错
- js排他功能示例
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- ajax-jq
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 二进制中1的个数(Java实现)
问题: 输入一个整数,求其二进制中1的个数 看到这个问题,我们应该想到数的位运算: 解法一:我们每次将此数&1 ,如果结果等于1,证明此数的最后一位是1,,count++: 然后在将数右移一位 ...
- WPS Office for Mac如何修改Word文档文字排列?WPS office修改Word文档文字排列方向教程
Word文档如何改变文字的排列方向?最新版WPS Office for Mac修复了文字排版相关的细节问题,可以更快捷的进行Word编辑,WPS Office在苹果电脑中如何修改Word文档文字排列方 ...