iOS开发学习-类似微信聊天消息中的电话号码点击保存到通讯录中的功能
类似微信聊天消息中的电话号码点击保存到通讯录中的功能,ABAddress的实现在iOS9中是不能正常使用的,点击完成后,手机会非常的卡,iOS9之后需要使用Contact新提供的方法来实现该功能。快捷保存手机号码到系统通讯录中的需求在很多的应用中都会用的到,QQ、微信等社交软件都是可以见到的,虽然实现起来也是很简单的,小编还是把这个小功能整理一下,方便后面在需要的时候能方便的使用,也能方便朋友们能感到方便。有需要的直接可以拿去,甚是方便,废话不多说,代码已经上传Github:https://github.com/Gjianhao/JHContacts
ViewController.h
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h> @interface ViewController : UIViewController<UIActionSheetDelegate,ABNewPersonViewControllerDelegate,ABPeoplePickerNavigationControllerDelegate,CNContactPickerDelegate,CNContactViewControllerDelegate> @property (nonatomic, strong)CNContactViewController *controller; @end
ViewController.m
#import "ViewController.h" #define iOS7Later ([UIDevice currentDevice].systemVersion.floatValue >= 7.0f)
#define iOS8Later ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f)
#define iOS9Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.0f)
#define iOS9_1Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.1f) @interface ViewController (){
NSString *linkMobile;
}
/**
* 电话号码
*/
@property (nonatomic, weak) IBOutlet UIButton *btnNum; - (IBAction)btnNumClick:(id)sender; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (IBAction)btnNumClick:(id)sender {
linkMobile = _btnNum.titleLabel.text;
NSString *title = [NSString stringWithFormat:@"%@可能是一个电话号码,你可以",linkMobile];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"呼叫",@"添加到手机通讯录", nil];
actionSheet.tag=;
[actionSheet showInView:self.view];
}
#pragma mark - UIActionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (actionSheet.tag==) {
if(buttonIndex==){
NSURL *tmpUrl=[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",linkMobile]];
[[UIApplication sharedApplication]openURL:tmpUrl];
}
else if(buttonIndex==){
NSString *title = [NSString stringWithFormat:@"%@可能是一个电话号码,你可以",linkMobile];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"创建新联系人",@"添加到现有联系人", nil];
actionSheet.tag=;
[actionSheet showInView:self.view];
}
} else if (actionSheet.tag==){
if (buttonIndex==) {
if (iOS9Later) {
//1.创建Contact对象,须是可变
CNMutableContact *contact = [[CNMutableContact alloc] init];
//2.为contact赋值
[self setValueForContact:contact existContect:NO];
//3.创建新建联系人页面
_controller = [CNContactViewController viewControllerForNewContact:contact];
_controller.navigationItem.title = @"新建联系人";
//代理内容根据自己需要实现
_controller.delegate = self;
//4.跳转
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_controller];
[self presentViewController:nc animated:YES completion:nil];
} else {
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
ABRecordRef newPerson = ABPersonCreate();
ABMutableMultiValueRef multiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
CFErrorRef error = NULL; ABMultiValueAddValueAndLabel(multiValue, (__bridge CFTypeRef)(linkMobile), kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiValue , &error);
picker.displayedPerson = newPerson;
picker.newPersonViewDelegate = self;
picker.navigationItem.title = @"新建联系人";
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:picker];
[self presentViewController:nc animated:YES completion:nil];
CFRelease(newPerson);
CFRelease(multiValue);
}
} else if (buttonIndex==) {
if (iOS9Later) {
//1.跳转到联系人选择页面,注意这里没有使用UINavigationController
CNContactPickerViewController *controller = [[CNContactPickerViewController alloc] init];
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
} else {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
} }
}
}
#pragma mark - iOS9以前的ABNewPersonViewController代理方法
/* 该代理方法可dismiss新添联系人页面 */
-(void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person { [newPersonView dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - iOS9以前的ABPeoplePickerNavigationController的代理方法
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person { [peoplePicker dismissViewControllerAnimated:YES completion:^{
/* 获取联系人电话 */
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSMutableArray *phones = [NSMutableArray array];
for (CFIndex i = ; i < ABMultiValueGetCount(phoneMulti); i++) {
NSString *aPhone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i);
NSString *aLabel = (__bridge_transfer NSString*)ABMultiValueCopyLabelAtIndex(phoneMulti, i);
NSLog(@"手机号标签:%@ 手机号:%@",aLabel,aPhone);
[phones addObject:aPhone];
[phones addObject:aLabel];
}
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
ABMutableMultiValueRef multiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
CFErrorRef error = NULL; ABMultiValueAddValueAndLabel(multiValue, (__bridge CFTypeRef)(linkMobile), kABPersonPhoneMobileLabel, NULL);
for (int i = ; i<[phones count]; i+=) { ABMultiValueAddValueAndLabel(multiValue, (__bridge CFTypeRef)([phones objectAtIndex:i]), (__bridge CFStringRef)([phones objectAtIndex:i+]), NULL);
}
ABRecordSetValue(person, kABPersonPhoneProperty, multiValue, &error);
picker.displayedPerson = person;
picker.newPersonViewDelegate = self;
picker.navigationItem.title = @"新建联系人";
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:picker];
[self presentViewController:nc animated:YES completion:nil];
CFRelease(multiValue);
CFRelease(phoneMulti);
}];
} #pragma mark - iOS9以后的CNContactViewControllerDelegate代理方法
/* 该代理方法可dismiss新添联系人页面 */
- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(CNContact *)contact { [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - iOS9以后的CNContactPickerDelegate的代理方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{ [picker dismissViewControllerAnimated:YES completion:^{ //3.copy一份可写的Contact对象,不能用alloc
CNMutableContact *con = [contact mutableCopy];
//4.为contact赋值
[self setValueForContact:con existContect:YES];
//5.跳转到新建联系人页面
CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:con];
controller.delegate = self;
controller.navigationItem.title = @"新建联系人";
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentViewController:nc animated:YES completion:nil];
}];
} /**
* 设置要保存的contact对象
*
* @param contact 联系人
* @param exist 是否需要重新创建
*/
- (void)setValueForContact:(CNMutableContact *)contact existContect:(BOOL)exist {
//电话
CNLabeledValue *phoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMobile value:[CNPhoneNumber phoneNumberWithStringValue:linkMobile]];
if (!exist) {
contact.phoneNumbers = @[phoneNumber];
} else {
//现有联系人情况
if ([contact.phoneNumbers count] >) {
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] initWithArray:contact.phoneNumbers];
[phoneNumbers addObject:phoneNumber];
contact.phoneNumbers = phoneNumbers;
}else{
contact.phoneNumbers = @[phoneNumber];
}
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
最后截图:
iOS开发学习-类似微信聊天消息中的电话号码点击保存到通讯录中的功能的更多相关文章
- IOS开发学习笔记043-QQ聊天界面实现
QQ聊天界面实现 效果如下: 实现过程: 1.首先实现基本界面 头像使用 UIImageView : 文字消息使用 UIButton 标签使用 UILable :水平居中 所有元素在一个cell中,在 ...
- IOS开发之——类似微信摇一摇的功能实现
首先,一直以为摇一摇的功能实现好高大上,结果百度了.我自己也模仿写了一个demo.主要代码如下: 新建一个项目,名字为AnimationShake. 主要代码: - (void)motionBegan ...
- 李洪强iOS开发之-环信04_消息
李洪强iOS开发之-环信04_消息 消息:IM 交互实体,在 SDK 中对应的类型是 EMMessage.EMMessage 由 EMMessageBody 组成. 构造消息 构造文字消息 EMT ...
- ios开发 学习积累20161027~20161031
前言 学习ios这几天来,总结下,函数的定义,调用.跟其他语言都有一定的区别: 几个特别重要的就是对象的迭代的使用和判断.取随机数.动画的实现及数组的深入研究等等 之前的总结地址 ios开发 学习积累 ...
- 类似微信聊天小程序-网易云信,IM DEMO小程序版本
类似微信聊天小程序-网易云信,IM DEMO小程序版本 代码地址: https://github.com/netease-im/NIM_Web_Weapp_Demo 云信IM DEMO 小程序版本 ( ...
- XMPPFrameWork IOS 开发(六)聊天室
原始地址:XMPPFrameWork IOS 开发(六)聊天室 聊天室 //初始化聊天室 XMPPJID *roomJID = [XMPPJID jidWithString:ROOM_JID]; xm ...
- MyEclipse项目中的文件点击右键Team选项中没有提交到SVN中的选项是怎么回事
MyEclipse项目中的文件点击右键Team选项中没有提交到SVN中的选项是怎么回事 其实你已经可以百度到很多方法: 例如下面博客提供的 http://www.xuebuyuan.com/95285 ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- iOS 即时通讯 + 仿微信聊天框架 + 源码
这些你造吗? 即时通讯(IM),在IOS这片江湖里面已经算是一个老者了,我这小旋风也是在很早以前巡山的时候,就知道有即时通讯这个妖怪,以前也多多少少接触过一些,在造APP的时候用过,哎呀,说着说着就感 ...
随机推荐
- SICP 习题 (1.35)解题总结
SICP 习题 1.35要求我们证明黄金切割率φ 是变换函数 x => 1+ 1/x 的不动点,然后利用这一事实通过过程fixed-point 计算出φ的值. 首先是有关函数的不动点,这个概念须 ...
- MySQL 基础回顾
mysql 回顾 数据库的设计必须满足三范式 1NF: 强调列的原子性,列不可拆分 eg: 一张表(联系人) 有(姓名,性别,电话)三列,但是现实中电话又可分为家庭电话和公司电话,这种表结构设计就不符 ...
- Redis数据类型基本操作
String类型: 设置键值对: set key value 设置键值对和过期时间:setex key seconds value ( 以秒为单位 ) 设置多个键值对: mset key1 value ...
- Phpstorm如何连接服务器
当服务器是Linux的时候不懂指令觉得很懊恼,这个时候直接就可以使用PHPstorm连接服务器操作了: 1丶准备工作 首先你先要准备服务器丶phpstorm这两个吧! 2丶开始配置phpstorm 按 ...
- STM32中EXTI和NVIC的关系
(1)NVIC(嵌套向量中断):NVIC是Cortex-M3核心的一部分,关于它的资料不在<STM32的技术参考手册>中,应查阅ARM公司的<Cortex-M3技术参考手册>C ...
- 20155332 补交ch12课下作业
20155332 补交ch12课下作业 课下测试提交晚了,我课后补做了一遍,答对13题,答错3题. 试题内容如下所示: 课本内容 1.并发(Concurrency) 访问慢I/O设备:就像当应用程序等 ...
- 20145226夏艺华 《Java程序设计》 课堂实践
手速慢了一秒,泪流成河...打水印的时间用太多了 /** * Created by XiaYihua on 2017/5/31. */ import java.io.*; public class F ...
- vab set dim
'问题一'给普通变量赋值使用LET ,只是LET 可以省略.'给对象变量赋值使用SET,SET 不能省略. Sub AA() Dim arr As String arr = "h ...
- python基础学习1-SET 集合
# -*- coding:utf-8 -*- set集合 无序不重复的序列 se = {"a","b","c"} #创建SET集合 prin ...
- Scikit-Learn机器学习入门
现在最常用的数据分析的编程语言为R和Python.每种语言都有自己的特点,Python因为Scikit-Learn库赢得了优势.Scikit-Learn有完整的文档,并实现很多机器学习算法,而每种算法 ...