forked from twistle/ABContactHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
executable file
·166 lines (138 loc) · 5.37 KB
/
main.m
File metadata and controls
executable file
·166 lines (138 loc) · 5.37 KB
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
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 5.x Edition
BSD License, Use at your own risk
*/
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "ABContact.h"
#import "ABContactsHelper.h"
#import "ABStandin.h"
#import "ModalAlertDelegate.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]
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
@interface TestBedViewController : UITableViewController <ABNewPersonViewControllerDelegate, ABPeoplePickerNavigationControllerDelegate>
@property (strong, nonatomic) NSArray *contacts;
@end
@implementation TestBedViewController
- (void)viewDidLoad {
[super viewDidLoad];
_contacts = [ABContactsHelper contacts];
}
#pragma mark - UITableViewDataSource
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _contacts.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ABContactCell"];
cell.textLabel.text = [_contacts[indexPath.row] compositeName];
return cell;
}
- (BOOL) ask: (NSString *) aQuestion
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:aQuestion message:nil delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Okay", nil];
ModalAlertDelegate *delegate = [ModalAlertDelegate delegateWithAlert:alertView];
int response = [delegate show];
return response;
}
#pragma mark NEW PERSON DELEGATE METHODS
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
if (person)
{
ABContact *contact = [ABContact contactWithRecord:person];
self.title = [NSString stringWithFormat:@"Added %@", contact.compositeName];
NSError *error;
BOOL success = [ABContactsHelper addContact:contact withError:&error];
if (!success)
{
NSLog(@"Could not add contact. %@", error.localizedFailureReason);
self.title = @"Error.";
}
[ABStandin save:nil];
}
else
self.title = @"Cancelled";
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark PEOPLE PICKER DELEGATE METHODS
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
[self dismissModalViewControllerAnimated:YES];
ABContact *contact = [ABContact contactWithRecord:person];
NSString *query = [NSString stringWithFormat:@"Really delete %@?", contact.compositeName];
if ([self ask:query])
{
self.title = [NSString stringWithFormat:@"Deleted %@", contact.compositeName];
[contact removeSelfFromAddressBook:nil];
[ABStandin save:nil];
}
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
// required method that is never called in the people-only-picking
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
- (void) add
{
// create a new view controller
ABNewPersonViewController *npvc = [[ABNewPersonViewController alloc] init];
// Create a new contact
ABContact *contact = [ABContact contact];
npvc.displayedPerson = contact.record;
// Set delegate
npvc.newPersonViewDelegate = self;
[self.navigationController pushViewController:npvc animated:YES];
}
- (void) remove
{
ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init];
ppnc.peoplePickerDelegate = self;
[self presentModalViewController:ppnc animated:YES];
}
- (void) loadView
{
[super loadView];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Add", @selector(add));
self.navigationItem.leftBarButtonItem = BARBUTTON(@"Remove", @selector(remove));
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
#pragma mark -
#pragma mark Application Setup
@interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
}
@end
@implementation TestBedAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application setStatusBarHidden:YES];
[[UINavigationBar appearance] setTintColor:COOKBOOK_PURPLE_COLOR];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
TestBedViewController *tbvc = [[TestBedViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tbvc];
window.rootViewController = nav;
[window makeKeyAndVisible];
return YES;
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");
return retVal;
}
}