Clear the contacts cache if access to the system contacts is revoked.

pull/1/head
Matthew Chen 7 years ago
parent 295f720f93
commit 01cf2fc1bd

@ -134,7 +134,7 @@ NSString *const OWSContactsManagerSignalAccountsDidChangeNotification
return self.systemContactsFetcher.supportsContactEditing; return self.systemContactsFetcher.supportsContactEditing;
} }
#pragma mark SystemContactsFetcherDelegate #pragma mark - SystemContactsFetcherDelegate
- (void)systemContactsFetcher:(SystemContactsFetcher *)systemsContactsFetcher - (void)systemContactsFetcher:(SystemContactsFetcher *)systemsContactsFetcher
updatedContacts:(NSArray<Contact *> *)contacts updatedContacts:(NSArray<Contact *> *)contacts
@ -152,6 +152,16 @@ NSString *const OWSContactsManagerSignalAccountsDidChangeNotification
[self updateWithContacts:contacts shouldClearStaleCache:shouldClearStaleCache]; [self updateWithContacts:contacts shouldClearStaleCache:shouldClearStaleCache];
} }
- (void)systemContactsFetcher:(SystemContactsFetcher *)systemContactsFetcher
hasAuthorizationStatus:(enum ContactStoreAuthorizationStatus)authorizationStatus
{
if (authorizationStatus == ContactStoreAuthorizationStatusRestricted
|| authorizationStatus == ContactStoreAuthorizationStatusDenied) {
// Clear the contacts cache if access to the system contacts is revoked.
[self updateWithContacts:@[] shouldClearStaleCache:YES];
}
}
#pragma mark - Intersection #pragma mark - Intersection
- (void)intersectContactsWithCompletion:(void (^)(NSError *_Nullable error))completionBlock - (void)intersectContactsWithCompletion:(void (^)(NSError *_Nullable error))completionBlock

@ -109,7 +109,8 @@ class ContactsFrameworkContactStoreAdaptee: ContactStoreAdaptee {
} }
} }
public enum ContactStoreAuthorizationStatus { @objc
public enum ContactStoreAuthorizationStatus: UInt {
case notDetermined, case notDetermined,
restricted, restricted,
denied, denied,
@ -118,6 +119,7 @@ public enum ContactStoreAuthorizationStatus {
@objc public protocol SystemContactsFetcherDelegate: class { @objc public protocol SystemContactsFetcherDelegate: class {
func systemContactsFetcher(_ systemContactsFetcher: SystemContactsFetcher, updatedContacts contacts: [Contact], isUserRequested: Bool) func systemContactsFetcher(_ systemContactsFetcher: SystemContactsFetcher, updatedContacts contacts: [Contact], isUserRequested: Bool)
func systemContactsFetcher(_ systemContactsFetcher: SystemContactsFetcher, hasAuthorizationStatus authorizationStatus: ContactStoreAuthorizationStatus)
} }
@objc @objc
@ -212,12 +214,13 @@ public class SystemContactsFetcher: NSObject {
self.contactStoreAdapter.requestAccess { (granted, error) in self.contactStoreAdapter.requestAccess { (granted, error) in
if let error = error { if let error = error {
Logger.error("\(self.TAG) error fetching contacts: \(error)") Logger.error("\(self.TAG) error fetching contacts: \(error)")
Logger.flush()
completion(error) completion(error)
return return
} }
guard granted else { guard granted else {
// This case should have been caught be the error guard a few lines up. // This case should have been caught by the error guard a few lines up.
owsFail("\(self.TAG) declined contact access.") owsFail("\(self.TAG) declined contact access.")
completion(nil) completion(nil)
return return
@ -231,6 +234,7 @@ public class SystemContactsFetcher: NSObject {
self.updateContacts(completion: completion) self.updateContacts(completion: completion)
case .denied, .restricted: case .denied, .restricted:
Logger.debug("\(TAG) contacts were \(self.authorizationStatus)") Logger.debug("\(TAG) contacts were \(self.authorizationStatus)")
self.delegate?.systemContactsFetcher(self, hasAuthorizationStatus: authorizationStatus)
completion(nil) completion(nil)
} }
} }
@ -239,6 +243,7 @@ public class SystemContactsFetcher: NSObject {
public func fetchOnceIfAlreadyAuthorized() { public func fetchOnceIfAlreadyAuthorized() {
SwiftAssertIsOnMainThread(#function) SwiftAssertIsOnMainThread(#function)
guard authorizationStatus == .authorized else { guard authorizationStatus == .authorized else {
self.delegate?.systemContactsFetcher(self, hasAuthorizationStatus: authorizationStatus)
return return
} }
guard !systemContactsHaveBeenRequestedAtLeastOnce else { guard !systemContactsHaveBeenRequestedAtLeastOnce else {
@ -253,6 +258,7 @@ public class SystemContactsFetcher: NSObject {
SwiftAssertIsOnMainThread(#function) SwiftAssertIsOnMainThread(#function)
guard authorizationStatus == .authorized else { guard authorizationStatus == .authorized else {
owsFail("should have already requested contact access") owsFail("should have already requested contact access")
self.delegate?.systemContactsFetcher(self, hasAuthorizationStatus: authorizationStatus)
return return
} }
@ -279,6 +285,8 @@ public class SystemContactsFetcher: NSObject {
let completion: (Error?) -> Void = { error in let completion: (Error?) -> Void = { error in
DispatchMainThreadSafe({ DispatchMainThreadSafe({
completionParam?(error) completionParam?(error)
assert(backgroundTask != nil)
backgroundTask = nil backgroundTask = nil
}) })
} }

Loading…
Cancel
Save