diff --git a/AmiKo-Info.plist b/AmiKo-Info.plist
index 805ee7f..84d01c7 100644
--- a/AmiKo-Info.plist
+++ b/AmiKo-Info.plist
@@ -53,6 +53,8 @@
NSAllowsArbitraryLoads
+ NSContactsUsageDescription
+ Access Contact List for Patient List
NSHumanReadableCopyright
Copyright © 2020 ywesee GmbH. All rights reserved.
NSMainNibFile
diff --git a/CoMed-Info.plist b/CoMed-Info.plist
index 2323d1d..77fffef 100644
--- a/CoMed-Info.plist
+++ b/CoMed-Info.plist
@@ -51,6 +51,8 @@
NSAllowsArbitraryLoads
+ NSContactsUsageDescription
+ Access Contact List for Patient List
NSHumanReadableCopyright
Copyright © 2020 ywesee GmbH. All rights reserved.
NSMainNibFile
diff --git a/MLContacts.m b/MLContacts.m
index 2864109..c020d0f 100644
--- a/MLContacts.m
+++ b/MLContacts.m
@@ -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;
diff --git a/MLPatientSheetController.m b/MLPatientSheetController.m
index 63da312..24dc62d 100644
--- a/MLPatientSheetController.m
+++ b/MLPatientSheetController.m
@@ -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