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的时候用过,哎呀,说着说着就感 ...
随机推荐
- 你知道CAN/RS-485总线为什么要隔离吗?
您在使用CAN或RS-485总线进行调试时,是否遇到过偶尔通信出错?或者接收不到数据?一直正常使用的总线,突然出现大范围的错误,或者节点损坏?您还在为这些问题不知所措,摸不着头脑吗?使用总线隔离,或许 ...
- U盘安装咱中国人自己的操作系统UbuntuKylin14.04LST(超具体原创图文教程)
本文仅供參考,在准备级安装过程中出现的一切意外情况均与本文作者无关!原创教程转载请注明原转载地!系统简单介绍:UbuntuKylin 是Ubuntu官方认可的衍生版,其宗旨是创建一个Ubuntu的中文 ...
- java 工作流项目源码 SSM 框架 Activiti-master springmvc 有手机端功能
即时通讯:支持好友,群组,发图片.文件,消息声音提醒,离线消息,保留聊天记录 (即时聊天功能支持手机端,详情下面有截图) 工作流模块---------------------------------- ...
- js call().apply().bind()的用法
function Person(age) { this.age = age; } Person.prototype.sayHi = function (x, y) { console.log((x + ...
- C++程序设计入门(上) 之对象和类
面向对象编程: 如何定义对象? 同类型对象用一 个通用的类来定义 class C { int p; int f(); }; C ca, cb; 一个类用变量来定义数据域,用函数定义行为. class ...
- 腾讯云Mac图床插件
背景 随着博客越写越多,难免会遇到需要插入图片来说明的情况. 图床选择 首先调研了市面上的图床服务,本着稳定长期的目标,过滤掉了打一枪换一个地方的野鸡小网站,剩余比较靠谱的优缺点如下. 图床 优点 缺 ...
- windows下beautifulsoup使用lxml解析使用报错
s4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need ...
- python科学计算和可视化学习报告
一丶numpy和matplotlib学习笔记 1. NumPy是Python语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库.Numpy内部解除了Pyth ...
- Ajax第二天——JQuery的Ajax
JQuery中的Ajax jQuery 对 Ajax 操作进行了封装, 在 jQuery 中最底层的方法是 $.ajax(), 第二层是 load(), $.get() 和 $.post(), (常 ...
- flex作图
<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="htt ...