-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContactsUI+Convenience.m
234 lines (166 loc) · 7.08 KB
/
ContactsUI+Convenience.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
//
// ContactsUI+Convenience.m
// vCard
//
// Created by Alexander Ivanov on 07.04.2018.
// Copyright © 2018 Alexander Ivanov. All rights reserved.
//
#import "ContactsUI+Convenience.h"
@implementation CNContactStore (Convenience)
__static(CNContactStore *, defaultStore, [[CNContactStore alloc] init])
+ (CNAuthorizationStatus)authorizationStatus {
return [self authorizationStatusForEntityType:CNEntityTypeContacts];
}
- (void)requestAccess:(void (^)(BOOL))completionHandler {
[self requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (completionHandler)
completionHandler(granted);
[error log:@"requestAccessForEntityType:"];
}];
}
- (BOOL)enumerateContactsWithKeysToFetch:(NSArray <id<CNKeyDescriptor>>*)keysToFetch usingBlock:(BOOL (^)(CNContact *))block {
if (!block)
return NO;
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
NSError *error = Nil;
BOOL result = [self enumerateContactsWithFetchRequest:request error:&error usingBlock:^(CNContact *contact, BOOL *stop) {
BOOL result = block(contact);
*stop = result;
}];
[error log:@"enumerateContactsWithFetchRequest:"];
return result;
}
@end
@implementation CNContact (Convenience)
- (NSString *)fullName {
return [CNContactFormatter stringFromContact:self style:CNContactFormatterStyleFullName];
}
- (NSString *)phoneticFullName {
return [CNContactFormatter stringFromContact:self style:CNContactFormatterStylePhoneticFullName];
}
- (UIImage *)image {
return self.imageData ? [UIImage imageWithData:self.imageData] : Nil;
}
- (UIImage *)thumbnailImage {
return self.imageData ? [UIImage imageWithData:self.thumbnailImageData] : Nil;
}
- (CNSocialProfile *)socialProfileWithService:(NSString *)service {
return service ? [self.socialProfiles firstObject:^BOOL(CNLabeledValue<CNSocialProfile *> *obj) {
return [obj.value.service isEqualToString:service];
}].value : Nil;
}
- (CNInstantMessageAddress *)instantMessageAddressWithService:(NSString *)service {
return service ? [self.instantMessageAddresses firstObject:^BOOL(CNLabeledValue<CNInstantMessageAddress *> *obj) {
return [obj.value.service isEqualToString:service];
}].value : Nil;
}
@end
@implementation CNContactVCardSerialization (Convenience)
+ (NSArray<CNContact *> *)contactsWithData:(NSData *)data {
if (!data)
return Nil;
NSError *error = Nil;
NSArray<CNContact *> *contacts = [self contactsWithData:data error:&error];
[error log:@"dataWithContacts:"];
return contacts;
}
+ (NSData *)dataWithContacts:(NSArray<CNContact *> *)contacts {
if (!contacts)
return Nil;
NSError *error = Nil;
NSData *data = [self dataWithContacts:contacts error:&error];
[error log:@"dataWithContacts:"];
return data;
}
@end
@implementation CNSocialProfile (Convenience)
+ (instancetype)facebookProfileWithUsername:(NSString *)username {
return [[CNSocialProfile alloc] initWithUrlString:[NSString stringWithFormat:@"http://www.facebook.com/%@", username] username:username userIdentifier:Nil service:CNSocialProfileServiceFacebook];
}
+ (instancetype)flickrProfileWithUsername:(NSString *)username {
return [[CNSocialProfile alloc] initWithUrlString:[NSString stringWithFormat:@"http://www.flickr.com/photos/%@", username] username:username userIdentifier:Nil service:CNSocialProfileServiceFlickr];
}
+ (instancetype)linkedInProfileWithUsername:(NSString *)username {
return [[CNSocialProfile alloc] initWithUrlString:[NSString stringWithFormat:@"http://www.linkedin.com/in/%@", username] username:username userIdentifier:Nil service:CNSocialProfileServiceLinkedIn];
}
+ (instancetype)twitterProfileWithUsername:(NSString *)username {
return [[CNSocialProfile alloc] initWithUrlString:[NSString stringWithFormat:@"http://twitter.com/%@", username] username:username userIdentifier:Nil service:CNSocialProfileServiceTwitter];
}
- (NSURL *)url {
return [NSURL URLWithString:self.urlString];
}
@end
@implementation NSArray (CNLabeledValue)
- (NSArray *)allLabeled:(NSString *)label {
return [self query:^BOOL(id obj) {
return [obj isKindOfClass:[CNLabeledValue class]] ? [((CNLabeledValue *)obj).label isEqualToString:label] : NO;
}];
}
- (id)firstLabeled:(NSString *)label {
return [self firstObject:^BOOL(id obj) {
return [obj isKindOfClass:[CNLabeledValue class]] ? [((CNLabeledValue *)obj).label isEqualToString:label] : NO;
}];
}
@end
@interface CNContactNavigationController : UINavigationController <CNContactViewControllerDelegate>
@property (strong, nonatomic, readonly) CNContactViewController *contactViewController;
@end
@implementation CNContactNavigationController
- (CNContactViewController *)contactViewController {
return self.viewControllers.firstObject;
}
+ (instancetype)viewControllerForUnknownContact:(CNContact *)contact {
CNContactViewController *vc = [CNContactViewController viewControllerForUnknownContact:contact];
CNContactNavigationController *nav = [[CNContactNavigationController alloc] initWithRootViewController:vc];
vc.delegate = nav;
vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nav action:@selector(done:)];
return nav;
}
- (IBAction)done:(UIBarButtonItem *)sender {
[self dismissViewControllerAnimated:YES completion:Nil];
}
- (BOOL)contactViewController:(CNContactViewController *)viewController shouldPerformDefaultActionForContactProperty:(CNContactProperty *)property {
return YES;
}
- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact {
[viewController.navigationController dismissViewControllerAnimated:YES completion:Nil];
}
@end
@interface CNContactPickerViewControllerEx : CNContactPickerViewController <CNContactPickerDelegate>
@property void (^completion)(CNContact *);
@end
@implementation CNContactPickerViewControllerEx
- (instancetype)init {
self = [super init];
if (self) {
self.delegate = self;
}
return self;
}
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
if (self.completion)
self.completion(Nil);
[picker dismissViewControllerAnimated:YES completion:Nil];
}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
if (self.completion)
self.completion(contact);
[picker dismissViewControllerAnimated:YES completion:Nil];
}
@end
@implementation UIViewController (ContactsUI)
- (CNContactPickerViewController *)presentContactPickerWithCompletion:(void (^)(CNContact *))completion {
CNContactPickerViewControllerEx *picker = [[CNContactPickerViewControllerEx alloc] init];
picker.completion = completion;
[self presentViewController:picker animated:YES completion:Nil];
return picker;
}
- (CNContactViewController *)presentUnknownContact:(CNContact *)contact store:(CNContactStore *)contactStore {
if (!contact)
return Nil;
CNContactNavigationController *vc = [CNContactNavigationController viewControllerForUnknownContact:contact];
vc.contactViewController.contactStore = contactStore;
[self presentViewController:vc animated:YES completion:Nil];
return vc.contactViewController;
}
@end