From 364f416a690404a19e3784ee19b44eacd6808ec7 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 3 May 2017 17:03:26 -0400 Subject: [PATCH] Block editing contact if user has denied contact permissions // FREEBIE --- .../ShowGroupMembersViewController.m | 34 +++++++++++++++++++ Signal/src/contact/OWSContactsManager.h | 8 +++++ Signal/src/contact/OWSContactsManager.m | 5 +++ .../src/contact/SystemContactsFetcher.swift | 10 ++++++ .../translations/en.lproj/Localizable.strings | 9 +++-- 5 files changed, 63 insertions(+), 3 deletions(-) diff --git a/Signal/src/ViewControllers/ShowGroupMembersViewController.m b/Signal/src/ViewControllers/ShowGroupMembersViewController.m index 2a33ab839..a790814bd 100644 --- a/Signal/src/ViewControllers/ShowGroupMembersViewController.m +++ b/Signal/src/ViewControllers/ShowGroupMembersViewController.m @@ -11,6 +11,7 @@ #import "SignalAccount.h" #import "SignalsViewController.h" #import "UIUtil.h" +#import "ViewControllerUtils.h" #import #import #import @@ -250,7 +251,39 @@ NS_ASSUME_NONNULL_BEGIN ContactsViewHelper *helper = self.contactsViewHelper; SignalAccount *signalAccount = [helper signalAccountForRecipientId:recipientId]; + if (!helper.contactsManager.isSystemContactsAuthorized) { + UIAlertController *alertController = [UIAlertController + alertControllerWithTitle:NSLocalizedString(@"EDIT_CONTACT_WITHOUT_CONTACTS_PERMISSION_ALERT_TITLE", comment + : @"Alert title for when the user has just tried to edit a " + @"contacts after declining to give Signal contacts " + @"permissions") + message:NSLocalizedString(@"EDIT_CONTACT_WITHOUT_CONTACTS_PERMISSION_ALERT_BODY", comment + : @"Alert body for when the user has just tried to edit a " + @"contacts after declining to give Signal contacts " + @"permissions") + preferredStyle:UIAlertControllerStyleAlert]; + + [alertController + addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"AB_PERMISSION_MISSING_ACTION_NOT_NOW", + @"Button text to dismiss missing contacts permission alert") + style:UIAlertActionStyleCancel + handler:nil]]; + + [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OPEN_SETTINGS_BUTTON", + @"Button text which opens the settings app") + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *_Nonnull action) { + NSURL *settingsUrl = [NSURL + URLWithString:UIApplicationOpenSettingsURLString]; + [[UIApplication sharedApplication] openURL:settingsUrl]; + }]]; + + [self presentViewController:alertController animated:YES completion:nil]; + return; + } + if (signalAccount) { + // FIXME This is broken until converted to Contacts framework. ABPersonViewController *view = [[ABPersonViewController alloc] init]; ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, nil); @@ -261,6 +294,7 @@ NS_ASSUME_NONNULL_BEGIN [self.navigationController pushViewController:view animated:YES]; } else { + // FIXME This is broken until converted to Contacts framework. ABUnknownPersonViewController *view = [[ABUnknownPersonViewController alloc] init]; ABRecordRef aContact = ABPersonCreate(); diff --git a/Signal/src/contact/OWSContactsManager.h b/Signal/src/contact/OWSContactsManager.h index 315373abe..574e8bc94 100644 --- a/Signal/src/contact/OWSContactsManager.h +++ b/Signal/src/contact/OWSContactsManager.h @@ -31,7 +31,15 @@ extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification; #pragma mark - System Contact Fetching +// Must call `requestSystemContactsOnce` before accessing this method +@property (nonatomic, readonly) BOOL isSystemContactsAuthorized; + +// Request systems contacts and start syncing changes. The user will see an alert +// if they haven't previously. - (void)requestSystemContactsOnce; + +// Ensure's the app has the latest contacts, but won't prompt the user for contact +// access if they haven't granted it. - (void)fetchSystemContactsIfAlreadyAuthorized; // TODO: Remove this method. diff --git a/Signal/src/contact/OWSContactsManager.m b/Signal/src/contact/OWSContactsManager.m index d2bbb303f..642e33586 100644 --- a/Signal/src/contact/OWSContactsManager.m +++ b/Signal/src/contact/OWSContactsManager.m @@ -62,6 +62,11 @@ NSString *const OWSContactsManagerSignalAccountsDidChangeNotification = [self.systemContactsFetcher fetchIfAlreadyAuthorized]; } +- (BOOL)isSystemContactsAuthorized +{ + return self.systemContactsFetcher.isAuthorized; +} + #pragma mark SystemContactsFetcherDelegate - (void)systemContactsFetcher:(SystemContactsFetcher *)systemsContactsFetcher diff --git a/Signal/src/contact/SystemContactsFetcher.swift b/Signal/src/contact/SystemContactsFetcher.swift index 504927524..29bf1bcba 100644 --- a/Signal/src/contact/SystemContactsFetcher.swift +++ b/Signal/src/contact/SystemContactsFetcher.swift @@ -20,6 +20,16 @@ class SystemContactsFetcher: NSObject { return CNContactStore.authorizationStatus(for: CNEntityType.contacts) } + public var isAuthorized: Bool { + guard self.authorizationStatus != .notDetermined else { + assertionFailure("should have called `requestOnce` before this point.") + Logger.error("\(TAG) should have called `requestOnce` before checking authorization status.") + return false + } + + return self.authorizationStatus == .authorized + } + private let contactStore = CNContactStore() private var systemContactsHaveBeenRequestedAtLeastOnce = false private let allowedContactKeys: [CNKeyDescriptor] = [ diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index 552b7e82a..2a0527a87 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -1,9 +1,6 @@ /* Button text to dismiss missing contacts permission alert */ "AB_PERMISSION_MISSING_ACTION_NOT_NOW" = "Not Now"; -/* No comment provided by engineer. */ -"AB_PERMISSION_MISSING_BODY" = "Signal requires access to your contacts. We do not store your contacts on our servers."; - /* Alert title when contacts disabled */ "AB_PERMISSION_MISSING_TITLE" = "Sorry!"; @@ -328,6 +325,12 @@ /* Generic short text for button to dismiss a dialog */ "DISMISS_BUTTON_TEXT" = "Dismiss"; +/* Alert body for when the user has just tried to edit a contacts after declining to give Signal contacts permissions */ +"EDIT_CONTACT_WITHOUT_CONTACTS_PERMISSION_ALERT_BODY" = "You can give access in the Settings app."; + +/* Alert title for when the user has just tried to edit a contacts after declining to give Signal contacts permissions */ +"EDIT_CONTACT_WITHOUT_CONTACTS_PERMISSION_ALERT_TITLE" = "Signal Needs Contact Access to Edit Contact Information"; + /* table cell label in conversation settings */ "EDIT_GROUP_ACTION" = "Edit Group";