类似微信聊天消息中的电话号码点击保存到通讯录中的功能,ABAddress的实现在iOS9中是不能正常使用的,点击完成后,手机会非常的卡,iOS9之后需要使用Contact新提供的方法来实现该功能。快捷保存手机号码到系统通讯录中的需求在很多的应用中都会用的到,QQ、微信等社交软件都是可以见到的,虽然实现起来也是很简单的,小编还是把这个小功能整理一下,方便后面在需要的时候能方便的使用,也能方便朋友们能感到方便。有需要的直接可以拿去,甚是方便,废话不多说,代码已经上传Github:https://github.com/Gjianhao/JHContacts
ViewController.h
1 #import <UIKit/UIKit.h>
2 #import <AddressBook/AddressBook.h>
3 #import <AddressBookUI/AddressBookUI.h>
4 #import <Contacts/Contacts.h>
5 #import <ContactsUI/ContactsUI.h>
6
7 @interface ViewController : UIViewController<UIActionSheetDelegate,ABNewPersonViewControllerDelegate,ABPeoplePickerNavigationControllerDelegate,CNContactPickerDelegate,CNContactViewControllerDelegate>
8
9 @property (nonatomic, strong)CNContactViewController *controller;
10
11 @end
ViewController.m
1 #import "ViewController.h"
2
3
4 #define iOS7Later ([UIDevice currentDevice].systemVersion.floatValue >= 7.0f)
5 #define iOS8Later ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f)
6 #define iOS9Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.0f)
7 #define iOS9_1Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.1f)
8
9 @interface ViewController (){
10 NSString *linkMobile;
11 }
12 /**
13 * 电话号码
14 */
15 @property (nonatomic, weak) IBOutlet UIButton *btnNum;
16
17 - (IBAction)btnNumClick:(id)sender;
18
19 @end
20
21 @implementation ViewController
22
23 - (void)viewDidLoad {
24 [super viewDidLoad];
25 // Do any additional setup after loading the view, typically from a nib.
26 }
27
28 - (IBAction)btnNumClick:(id)sender {
29 linkMobile = _btnNum.titleLabel.text;
30 NSString *title = [NSString stringWithFormat:@"%@可能是一个电话号码,你可以",linkMobile];
31 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"呼叫",@"添加到手机通讯录", nil];
32 actionSheet.tag=2000;
33 [actionSheet showInView:self.view];
34 }
35 #pragma mark - UIActionSheetDelegate
36 -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
37 if (actionSheet.tag==2000) {
38 if(buttonIndex==0){
39 NSURL *tmpUrl=[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",linkMobile]];
40 [[UIApplication sharedApplication]openURL:tmpUrl];
41 }
42 else if(buttonIndex==1){
43 NSString *title = [NSString stringWithFormat:@"%@可能是一个电话号码,你可以",linkMobile];
44 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"创建新联系人",@"添加到现有联系人", nil];
45 actionSheet.tag=3000;
46 [actionSheet showInView:self.view];
47 }
48 } else if (actionSheet.tag==3000){
49 if (buttonIndex==0) {
50 if (iOS9Later) {
51 //1.创建Contact对象,须是可变
52 CNMutableContact *contact = [[CNMutableContact alloc] init];
53 //2.为contact赋值
54 [self setValueForContact:contact existContect:NO];
55 //3.创建新建联系人页面
56 _controller = [CNContactViewController viewControllerForNewContact:contact];
57 _controller.navigationItem.title = @"新建联系人";
58 //代理内容根据自己需要实现
59 _controller.delegate = self;
60 //4.跳转
61 UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_controller];
62 [self presentViewController:nc animated:YES completion:nil];
63 } else {
64 ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
65 ABRecordRef newPerson = ABPersonCreate();
66 ABMutableMultiValueRef multiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
67 CFErrorRef error = NULL;
68
69 ABMultiValueAddValueAndLabel(multiValue, (__bridge CFTypeRef)(linkMobile), kABPersonPhoneMobileLabel, NULL);
70 ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiValue , &error);
71 picker.displayedPerson = newPerson;
72 picker.newPersonViewDelegate = self;
73 picker.navigationItem.title = @"新建联系人";
74 UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:picker];
75 [self presentViewController:nc animated:YES completion:nil];
76 CFRelease(newPerson);
77 CFRelease(multiValue);
78 }
79 } else if (buttonIndex==1) {
80 if (iOS9Later) {
81 //1.跳转到联系人选择页面,注意这里没有使用UINavigationController
82 CNContactPickerViewController *controller = [[CNContactPickerViewController alloc] init];
83 controller.delegate = self;
84 [self presentViewController:controller animated:YES completion:nil];
85 } else {
86 ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
87 picker.peoplePickerDelegate = self;
88 [self presentViewController:picker animated:YES completion:nil];
89 }
90
91 }
92 }
93 }
94 #pragma mark - iOS9以前的ABNewPersonViewController代理方法
95 /* 该代理方法可dismiss新添联系人页面 */
96 -(void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person {
97
98 [newPersonView dismissViewControllerAnimated:YES completion:nil];
99 }
100 #pragma mark - iOS9以前的ABPeoplePickerNavigationController的代理方法
101 -(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
102
103 [peoplePicker dismissViewControllerAnimated:YES completion:^{
104 /* 获取联系人电话 */
105 ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
106 NSMutableArray *phones = [NSMutableArray array];
107 for (CFIndex i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {
108 NSString *aPhone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i);
109 NSString *aLabel = (__bridge_transfer NSString*)ABMultiValueCopyLabelAtIndex(phoneMulti, i);
110 NSLog(@"手机号标签:%@ 手机号:%@",aLabel,aPhone);
111 [phones addObject:aPhone];
112 [phones addObject:aLabel];
113 }
114 ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
115 ABMutableMultiValueRef multiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
116 CFErrorRef error = NULL;
117
118 ABMultiValueAddValueAndLabel(multiValue, (__bridge CFTypeRef)(linkMobile), kABPersonPhoneMobileLabel, NULL);
119 for (int i = 0; i<[phones count]; i+=2) {
120
121 ABMultiValueAddValueAndLabel(multiValue, (__bridge CFTypeRef)([phones objectAtIndex:i]), (__bridge CFStringRef)([phones objectAtIndex:i+1]), NULL);
122 }
123 ABRecordSetValue(person, kABPersonPhoneProperty, multiValue, &error);
124 picker.displayedPerson = person;
125 picker.newPersonViewDelegate = self;
126 picker.navigationItem.title = @"新建联系人";
127 UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:picker];
128 [self presentViewController:nc animated:YES completion:nil];
129 CFRelease(multiValue);
130 CFRelease(phoneMulti);
131 }];
132 }
133
134 #pragma mark - iOS9以后的CNContactViewControllerDelegate代理方法
135 /* 该代理方法可dismiss新添联系人页面 */
136 - (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(CNContact *)contact {
137
138 [self dismissViewControllerAnimated:YES completion:nil];
139 }
140 #pragma mark - iOS9以后的CNContactPickerDelegate的代理方法
141 - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
142
143 [picker dismissViewControllerAnimated:YES completion:^{
144
145 //3.copy一份可写的Contact对象,不能用alloc
146 CNMutableContact *con = [contact mutableCopy];
147 //4.为contact赋值
148 [self setValueForContact:con existContect:YES];
149 //5.跳转到新建联系人页面
150 CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:con];
151 controller.delegate = self;
152 controller.navigationItem.title = @"新建联系人";
153 UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:controller];
154 [self presentViewController:nc animated:YES completion:nil];
155 }];
156 }
157
158 /**
159 * 设置要保存的contact对象
160 *
161 * @param contact 联系人
162 * @param exist 是否需要重新创建
163 */
164 - (void)setValueForContact:(CNMutableContact *)contact existContect:(BOOL)exist {
165 //电话
166 CNLabeledValue *phoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMobile value:[CNPhoneNumber phoneNumberWithStringValue:linkMobile]];
167 if (!exist) {
168 contact.phoneNumbers = @[phoneNumber];
169 } else {
170 //现有联系人情况
171 if ([contact.phoneNumbers count] >0) {
172 NSMutableArray *phoneNumbers = [[NSMutableArray alloc] initWithArray:contact.phoneNumbers];
173 [phoneNumbers addObject:phoneNumber];
174 contact.phoneNumbers = phoneNumbers;
175 }else{
176 contact.phoneNumbers = @[phoneNumber];
177 }
178 }
179 }
180
181 - (void)didReceiveMemoryWarning {
182 [super didReceiveMemoryWarning];
183 // Dispose of any resources that can be recreated.
184 }
185
186 @end
最后截图: