Skip to content

Commit

Permalink
Merge pull request #260 from b123400/fix-257
Browse files Browse the repository at this point in the history
Ask for permission before accessing contact book
  • Loading branch information
zdavatz authored Jul 12, 2023
2 parents b0f860d + 652e036 commit 1e22a72
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 65 deletions.
2 changes: 2 additions & 0 deletions AmiKo-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSContactsUsageDescription</key>
<string>Access Contact List for Patient List</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020 ywesee GmbH. All rights reserved.</string>
<key>NSMainNibFile</key>
Expand Down
2 changes: 2 additions & 0 deletions CoMed-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSContactsUsageDescription</key>
<string>Access Contact List for Patient List</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020 ywesee GmbH. All rights reserved.</string>
<key>NSMainNibFile</key>
Expand Down
119 changes: 60 additions & 59 deletions MLContacts.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,69 +49,70 @@ - (NSArray *) getAllContacts
- (NSArray *) addAllContactsToArray:(NSMutableArray *)arrayOfContacts
{
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusDenied) {
if (![CNContactStore class]) {
return @[];
}
CNContactStore *addressBook = [[CNContactStore alloc] init];
if (status != CNAuthorizationStatusAuthorized) {
NSLog(@"This app was refused permissions to contacts.");
return @[];
}

if ([CNContactStore class]) {
CNContactStore *addressBook = [[CNContactStore alloc] init];

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];
}
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]];
}
}
}];
// Sort alphabetically
if ([arrayOfContacts count]>0) {
NSSortDescriptor *nameSort = [NSSortDescriptor sortDescriptorWithKey:@"familyName" ascending:YES];
[arrayOfContacts sortUsingDescriptors:[NSArray arrayWithObject:nameSort]];
}

return arrayOfContacts;
Expand Down
36 changes: 30 additions & 6 deletions MLPatientSheetController.m
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,37 @@ - (IBAction) onShowContacts:(id)sender
mSearchFiltered = FALSE;
[mSearchKey setStringValue:@""];

CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (![CNContactStore class]) {
return;
}
if (mABContactsVisible==NO) {
MLContacts *contacts = [[MLContacts alloc] init];
// Retrieves contacts from address book
mArrayOfPatients = [contacts getAllContacts];
[mTableView reloadData];
mABContactsVisible = YES;
[self setNumPatients:[mArrayOfPatients count]];
CNContactStore *addressBook = [[CNContactStore alloc] init];
if (status != CNAuthorizationStatusAuthorized) {
[addressBook requestAccessForEntityType:CNEntityTypeContacts
completionHandler:^(BOOL granted, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[[NSAlert alertWithError:error] runModal];
}
if (granted) {
[self onShowContacts:sender];
}
});
}];
} else {
// Need to be in background queue because it may be slow to takes contacts
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) , ^{
MLContacts *contacts = [[MLContacts alloc] init];
// Retrieves contacts from address book
mArrayOfPatients = [contacts getAllContacts];
dispatch_async(dispatch_get_main_queue(), ^{
[mTableView reloadData];
mABContactsVisible = YES;
[self setNumPatients:[mArrayOfPatients count]];
});
});
}
}
else {
// Retrieves contacts from local patient database
Expand Down

0 comments on commit 1e22a72

Please sign in to comment.