-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMLContacts.m
121 lines (102 loc) · 5.52 KB
/
MLContacts.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
/*
Copyright (c) 2017 Max Lungarella <[email protected]>
Created on 30/06/2017.
This file is part of AmiKo for OSX.
AmiKo for OSX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------ */
#import "MLContacts.h"
#import "MLPatient.h"
@implementation MLContacts
{
NSMutableArray *groupOfContacts;
}
- (id) init
{
if (self = [super init]) {
return self;
}
return nil;
}
- (NSArray *) getAllContacts
{
groupOfContacts = [NSMutableArray array];
[self addAllContactsToArray:groupOfContacts];
return groupOfContacts;
}
- (NSArray *) addAllContactsToArray:(NSMutableArray *)arrayOfContacts
{
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (![CNContactStore class]) {
return @[];
}
CNContactStore *addressBook = [[CNContactStore alloc] init];
if (status != CNAuthorizationStatusAuthorized) {
NSLog(@"This app was refused permissions to contacts.");
return @[];
}
NSArray *keys = @[CNContactIdentifierKey,
CNContactFamilyNameKey,
CNContactGivenNameKey,
CNContactBirthdayKey,
CNContactPostalAddressesKey,
CNContactPhoneNumbersKey,
CNContactEmailAddressesKey];
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
NSError *error;
[addressBook enumerateContactsWithFetchRequest:request
error:&error
usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop) {
if (error) {
NSLog(@"error fetching contacts %@", error);
} else {
MLPatient *patient = [[MLPatient alloc] init];
patient.familyName = contact.familyName;
patient.givenName = contact.givenName;
// Postal address
patient.postalAddress = @"";
patient.zipCode = @"";
patient.city = @"";
patient.country = @"";
if ([contact.postalAddresses count]>0) {
CNPostalAddress *pa = [contact.postalAddresses[0] value];
patient.postalAddress = pa.street;
patient.zipCode = pa.postalCode;
patient.city = pa.city;
patient.country = pa.country;
}
// Email address
patient.emailAddress = @"";
if ([contact.emailAddresses count]>0)
patient.emailAddress = [contact.emailAddresses[0] value];
// Birthdate
patient.birthDate = @"";
if (contact.birthday.year>1900)
patient.birthDate = [NSString stringWithFormat:@"%ld-%ld-%ld", contact.birthday.day, contact.birthday.month, contact.birthday.year];
// Phone number
patient.phoneNumber = @"";
if ([contact.phoneNumbers count]>0)
patient.phoneNumber = [[contact.phoneNumbers[0] value] stringValue];
// Add only if patients names are meaningful
if ([patient.familyName length]>0 && [patient.givenName length]>0) {
patient.databaseType = eAddressBook;
[arrayOfContacts addObject:patient];
}
}
}];
// Sort alphabetically
if ([arrayOfContacts count]>0) {
NSSortDescriptor *nameSort = [NSSortDescriptor sortDescriptorWithKey:@"familyName" ascending:YES];
[arrayOfContacts sortUsingDescriptors:[NSArray arrayWithObject:nameSort]];
}
return arrayOfContacts;
}
@end