forked from playup/ABContactHelper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.m
executable file
·237 lines (198 loc) · 8.6 KB
/
main.m
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
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 3.0 Edition
BSD License, Use at your own risk
*/
#import <UIKit/UIKit.h>
#import "ABContactsHelper.h"
#define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
@interface TestBedViewController : UIViewController
{
NSMutableString *log;
IBOutlet UITextView *textView;
}
@property (retain) NSMutableString *log;
@end
@implementation TestBedViewController
@synthesize log;
- (void) doLog: (NSString *) formatstring, ...
{
va_list arglist;
if (!formatstring) return;
va_start(arglist, formatstring);
NSString *outstring = [[[NSString alloc] initWithFormat:formatstring arguments:arglist] autorelease];
va_end(arglist);
[self.log appendString:outstring];
[self.log appendString:@"\n"];
textView.text = self.log;
}
- (void) addGW
{
// Search for a contact, creating a new one if one is not found
NSArray *contacts = [ABContactsHelper contactsMatchingName:@"Washington" andName:@"George"];
printf("%d matching contacts found\n", contacts.count);
ABContact *peep = contacts.count ? [contacts lastObject] : [ABContact contact];
if (contacts.count)
{
NSError *error;
if (![peep removeSelfFromAddressBook:&error]) // remove in preparation to update contact
{
NSLog(@"Error: %@", [error localizedDescription]);
return;
}
}
printf("Record %d\n", peep.recordID);
// Person basic string information (see full list in ABContact)
peep.firstname = @"George";
peep.lastname = @"Washington";
peep.nickname = @"Prez";
peep.firstnamephonetic = @"Horhay";
peep.lastnamephonetic = @"Warsh-ing-town";
peep.jobtitle = @"President of the United States of America";
// Emails
NSMutableArray *emailarray = [NSMutableArray array];
[emailarray addObject:[ABContact dictionaryWithValue:@"george@home.com" andLabel:kABHomeLabel]];
[emailarray addObject:[ABContact dictionaryWithValue:@"george@work.com" andLabel:kABWorkLabel]];
[emailarray addObject:[ABContact dictionaryWithValue:@"george@gmail.com" andLabel:(CFStringRef) @"Google"]];
peep.emailDictionaries = emailarray;
// Phones
NSMutableArray *phonearray = [NSMutableArray array];
[phonearray addObject:[ABContact dictionaryWithValue:@"202-555-1212" andLabel:kABPersonPhoneMainLabel]];
[phonearray addObject:[ABContact dictionaryWithValue:@"202-555-1313" andLabel:(CFStringRef) @"Google"]];
[phonearray addObject:[ABContact dictionaryWithValue:@"202-555-1414" andLabel:kABPersonPhoneMobileLabel]];
peep.phoneDictionaries = phonearray;
// URLS
NSMutableArray *urls = [NSMutableArray array];
[urls addObject:[ABContact dictionaryWithValue:@"http://whitehouse.org" andLabel:kABPersonHomePageLabel]];
[urls addObject:[ABContact dictionaryWithValue:@"http://en.wikipedia.org/wiki/Washington" andLabel:kABOtherLabel]];
peep.urlDictionaries = urls;
// Dates
NSMutableArray *dates = [NSMutableArray array];
[dates addObject:[ABContact dictionaryWithValue:[NSDate dateWithTimeIntervalSinceNow:0] andLabel:(CFStringRef) @"Anniversary"]];
peep.dateDictionaries = dates;
// Addresses
NSMutableArray *addies = [NSMutableArray array];
NSDictionary *whaddy = [ABContact addressWithStreet:@"1600 Pennsylvania Avenue" withCity:@"Arlington" withState:@"Virginia" withZip:@"20202" withCountry:nil withCode:nil];
[addies addObject:[ABContact dictionaryWithValue:whaddy andLabel:kABWorkLabel]];
NSDictionary *bpaddy = [ABContact addressWithStreet:@"1 Main Street" withCity:@"Westmoreland" withState:@"Virginia" withZip:@"20333" withCountry:nil withCode:nil];
[addies addObject:[ABContact dictionaryWithValue:bpaddy andLabel:kABHomeLabel]];
peep.addressDictionaries = addies;
// SMSes
NSDictionary *sms = [ABContact smsWithService:kABPersonInstantMessageServiceAIM andUser:@"geow1735"];
NSMutableArray *smses = [NSMutableArray array];
[smses addObject:[ABContact dictionaryWithValue:sms andLabel:kABWorkLabel]];
peep.smsDictionaries = smses;
// Relationships (Not actually used on the iPhone, but here for the sake of example)
NSMutableArray *relatedarray = [NSMutableArray array];
[relatedarray addObject:[ABContact dictionaryWithValue:@"Mary Ball Washington" andLabel:kABPersonMotherLabel]];
[relatedarray addObject:[ABContact dictionaryWithValue:@"Augustine Washington" andLabel:kABPersonFatherLabel]];
peep.relatedNameDictionaries = relatedarray;
NSError *error;
if (![ABContactsHelper addContact:peep withError:&error]) // save to address book
NSLog(@"Error: %@", [error localizedDescription]);
}
- (void) scan
{
// Regular data dump
for (ABContact *person in [ABContactsHelper contacts])
{
printf("******\n");
printf("Name: %s\n", person.compositeName.UTF8String);
printf("Organization: %s\n", person.organization.UTF8String);
printf("Title: %s\n", person.jobtitle.UTF8String);
printf("Department: %s\n", person.department.UTF8String);
printf("Note: %s\n", person.note.UTF8String);
printf("Creation Date: %s\n", [person.creationDate description].UTF8String);
printf("Modification Date: %s\n", [person.modificationDate description].UTF8String);
printf("Emails: %s\n", [person.emailDictionaries description].UTF8String);
printf("Phones: %s\n", [person.phoneDictionaries description].UTF8String);
printf("URLs: %s\n", [person.urlDictionaries description].UTF8String);
printf("Addresses: %s\n\n", [person.addressDictionaries description].UTF8String);
}
// Example for matching phone numbers
for (ABContact *person in [ABContactsHelper contactsMatchingPhone:@"303"])
{
printf("******\n");
printf("Name: %s\n", person.compositeName.UTF8String);
printf("Phones: %s\n", person.phonenumbers.UTF8String);
}
// Example for matching names
for (ABContact *person in [ABContactsHelper contactsMatchingName:@"Jo"])
{
printf("******\n");
printf("Name: %s\n", person.contactName.UTF8String);
}
}
- (void) viewgroups
{
printf("There are %d groups\n", [ABContactsHelper numberOfGroups]);
for (ABGroup *group in [ABContactsHelper groups])
{
printf("Name: %s\n", group.name.UTF8String);
NSArray *members = group.members;
printf("Members: %d\n", members.count);
int n = 1;
for (ABContact *contact in members)
printf("%d: %s\n", n++, contact.compositeName.UTF8String);
}
}
- (void) addGroup
{
NSArray *groups = [ABContactsHelper groupsMatchingName:@"Everyone"];
printf("%d matching groups found\n", groups.count);
ABGroup *group = groups.count ? [groups lastObject] : [ABGroup group];
// Remove existing group to allow modification
if (groups.count) [group removeSelfFromAddressBook:nil];
// set name
group.name = @"Everyone";
// add members
NSArray *contacts = [ABContactsHelper contacts];
for (ABContact *contact in contacts)
[group addMember:contact withError:nil];
// Save the new or modified group
[ABContactsHelper addGroup:group withError:nil];
}
- (void) serialize
{
NSArray *contacts = [ABContactsHelper contactsMatchingName:@"Washington" andName:@"George"];
printf("%d matching contacts found\n", contacts.count);
ABContact *peep = contacts.count ? [contacts lastObject] : [ABContact contact];
if (!peep) return;
NSDictionary *dict = [peep dictionaryRepresentation];
CFShow(dict);
NSData *data = [peep dataRepresentation];
printf("%d bytes\n", data.length);
ABContact *contact = [ABContact contactWithData:data];
CFShow([contact dictionaryRepresentation]);
}
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
// BASIC TEST
// self.navigationItem.rightBarButtonItem = BARBUTTON(@"Add GW", @selector(addGW));
// self.navigationItem.leftBarButtonItem = BARBUTTON(@"Scan", @selector(scan));
// GROUPS TEST
// self.navigationItem.rightBarButtonItem = BARBUTTON(@"Groups", @selector(viewgroups));
// self.navigationItem.leftBarButtonItem = BARBUTTON(@"Add", @selector(addGroup));
// SERIALIZATION TEST
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Serialize", @selector(serialize));
}
@end
@interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
@end
@implementation TestBedAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[TestBedViewController alloc] init]];
[window addSubview:nav.view];
[window makeKeyAndVisible];
}
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");
[pool release];
return retVal;
}