iOS7 Programming Cookbook 第十三章学习笔记

####Adding Persons to Groups

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#import <AddressBook/AddressBook.h>

//访问通讯录被拒绝
NSString *const kDenied = @"访问通讯录被拒绝";
//进入通讯录也受到限制
NSString *const kRestricted = @"进入通讯录也受到限制";
//引用一个对象用于与通讯录数据库交互。
ABAddressBookRef addressBook;

@implementation AppDelegate
//弹出警告窗口
- (void) displayMessage:(NSString *)paramMessage{
[[[UIAlertView alloc] initWithTitle:nil
message:paramMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}

<!-- more -->

//新组名
- (ABRecordRef)newGroupWithName:(NSString *)paramGroupName inAddressBook:(ABAddressBookRef)paramAddressBook{

ABRecordRef result = NULL;

if (paramAddressBook == NULL){
NSLog(@"The address book is nil.");
return NULL;
}
//创建group
result = ABGroupCreate();

if (result == NULL){
NSLog(@"Failed to create a new group.");
return NULL;
}

BOOL couldSetGroupName = NO;
CFErrorRef error = NULL;
//集记录属性的值 返回bool
couldSetGroupName = ABRecordSetValue(result,
kABGroupNameProperty,
(__bridge CFTypeRef)paramGroupName,
&error);

if (couldSetGroupName){

BOOL couldAddRecord = NO;
CFErrorRef couldAddRecordError = NULL;
//添加一条记录一个地址簿。
couldAddRecord = ABAddressBookAddRecord(paramAddressBook,
result,
&couldAddRecordError);

if (couldAddRecord){

NSLog(@"成功添加组");
//显示地址簿是否有变化,没有保存到通讯录数据库。
if (ABAddressBookHasUnsavedChanges(paramAddressBook)){
BOOL couldSaveAddressBook = NO;
CFErrorRef couldSaveAddressBookError = NULL;
//保存
couldSaveAddressBook = ABAddressBookSave(paramAddressBook,
&couldSaveAddressBookError);
if (couldSaveAddressBook){
NSLog(@"保存成功");
} else {
CFRelease(result);
result = NULL;
NSLog(@"保存失败");
}
} else {
CFRelease(result);
result = NULL;
NSLog(@"未保存任何更改.");
}
} else {
CFRelease(result);
result = NULL;
NSLog(@"不能添加一个新的组");
}
} else {
CFRelease(result);
result = NULL;
NSLog(@"未能设置组的名称.");
}

return result;

}

//新的姓,名加入到addressBook
- (ABRecordRef) newPersonWithFirstName:(NSString *)paramFirstName
lastName:(NSString *)paramLastName
inAddressBook:(ABAddressBookRef)paramAddressBook{

ABRecordRef result = NULL;

if (paramAddressBook == NULL){
NSLog(@"The address book is NULL.");
return NULL;
}

if ([paramFirstName length] == 0 &&
[paramLastName length] == 0){
NSLog(@"姓名都为空");
return NULL;
}
//创建一个新的个人记录
result = ABPersonCreate();

if (result == NULL){
NSLog(@"未能创建一个记录");
return NULL;
}

//可以设置名
BOOL couldSetFirstName = NO;
//可以设置姓
BOOL couldSetLastName = NO;

CFErrorRef setFirstNameError = NULL;
CFErrorRef setLastNameError = NULL;
//集记录属性的值 返回bool
couldSetFirstName = ABRecordSetValue(result,
kABPersonFirstNameProperty,
(__bridge CFTypeRef)paramFirstName,
&setFirstNameError);
//集记录属性的值 返回bool
couldSetLastName = ABRecordSetValue(result,
kABPersonLastNameProperty,
(__bridge CFTypeRef)paramLastName,
&setLastNameError);

CFErrorRef couldAddPersonError = NULL;

BOOL couldAddPerson = ABAddressBookAddRecord(paramAddressBook,
result,
&couldAddPersonError);

if (couldAddPerson){
NSLog(@"成功添加");
} else {
NSLog(@"失败");
CFRelease(result);
result = NULL;
return result;
}

//显示地址簿是否有变化,没有保存到通讯录数据库。
if (ABAddressBookHasUnsavedChanges(paramAddressBook)){

CFErrorRef couldSaveAddressBookError = NULL;
//ABAddressBookSave 任何未保存的更改保存到通讯录数据库。
BOOL couldSaveAddressBook =
ABAddressBookSave(paramAddressBook,
&couldSaveAddressBookError);

if (couldSaveAddressBook){
NSLog(@"成功保存到通讯录");
} else {
NSLog(@"保存失败");
}
}

if (couldSetFirstName &&
couldSetLastName){
NSLog(@"成功设置姓名");
} else {
NSLog(@"设置姓名失败");
}

return result;

}

- (BOOL)addPerson:(ABRecordRef)paramPerson toGroup:(ABRecordRef)paramGroup saveToAddressBook:(ABAddressBookRef)paramAddressBook{
BOOL result = NO;

if (paramPerson == NULL || paramGroup == NULL || paramAddressBook == NULL){
NSLog(@"无效的参数");
return NO;
}

CFErrorRef error = NULL;

/* 尝试person加入*/
result = ABGroupAddMember(paramGroup, paramPerson, &error);

if (result == NO){
NSLog(@"不能添加到组的人");
return result;
}

/*确保我们保存任何未保存的更改 */
if (ABAddressBookHasUnsavedChanges(paramAddressBook)){
BOOL couldSaveAddressBook = NO;
CFErrorRef couldSaveAddressBookError = NULL;
couldSaveAddressBook = ABAddressBookSave(paramAddressBook,
&couldSaveAddressBookError);
if (couldSaveAddressBook){
NSLog(@"成功将person添加到group.");
result = YES;
} else {
NSLog(@"保存失败");
}
} else {
NSLog(@"没有变化.");
}

return result;

}

//添加人和组到addressbook
- (void)addPersonsAndGroupsToAddressBook:(ABAddressBookRef)paramAddressBook{
//引用记录
ABRecordRef richardBranson = [self newPersonWithFirstName:@"Richard" lastName:@"Branson" inAddressBook:paramAddressBook];
//不为空
if (richardBranson != NULL){

ABRecordRef entrepreneursGroup = [self newGroupWithName:@"Entrepreneurs" inAddressBook:paramAddressBook];

if (entrepreneursGroup != NULL){

if ([self addPerson:richardBranson
toGroup:entrepreneursGroup
saveToAddressBook:paramAddressBook]){

NSLog(@"成功将 Richard Branson 添加到Group");

} else {
NSLog(@"失败");
}

CFRelease(entrepreneursGroup);
} else {
NSLog(@"未能创建企业组.");
}

CFRelease(richardBranson);
} else {
NSLog(@"未能为Richard Branson创建一个实体");
}

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//CFError对象引用
CFErrorRef error = NULL;
//返回的授权状态访问通讯录数据的应用程序。
switch (ABAddressBookGetAuthorizationStatus()){
//应用程序被授权访问通讯录数据。
case kABAuthorizationStatusAuthorized:{
//创建一个新的通讯录从通讯录数据库对象和数据。
addressBook = ABAddressBookCreateWithOptions(NULL, &error);
[self addPersonsAndGroupsToAddressBook:addressBook];
if (addressBook != NULL){
//释放addressBook对象
CFRelease(addressBook);
}
break;
}
//用户明确否认了这个应用程序访问通讯录数据。
case kABAuthorizationStatusDenied:{
[self displayMessage:kDenied];
break;
}
//没有授权状态可以确定。
case kABAuthorizationStatusNotDetermined:{
addressBook = ABAddressBookCreateWithOptions(NULL, &error);
//从用户请求访问通讯录数据
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted){
//个人和组添加到通讯录
[self addPersonsAndGroupsToAddressBook:addressBook];
} else {
NSLog(@"没有授予访问");
}
if (addressBook != NULL){
//释放
CFRelease(addressBook);
}
});
break;
}
//这个应用程序未被授权访问通讯录数据。用户不能改变这种访问,可能是由于家长控制等的限制。
case kABAuthorizationStatusRestricted:{
[self displayMessage:kRestricted];
break;
}
}


self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%