From 8f1b2a6385b4839ae99b38897426c3a12ef76db9 Mon Sep 17 00:00:00 2001 From: Vijay Vikram Singh Date: Tue, 8 Oct 2019 07:25:40 -0700 Subject: [PATCH] fix(ios): support new property to remove note (#11248) Fixes TIMOB-27419 --- apidoc/Titanium/Contacts/Contacts.yml | 17 ++++++++++++++++- iphone/Classes/ContactsModule.h | 1 + iphone/Classes/ContactsModule.m | 24 +++++++++++++++++++++--- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/apidoc/Titanium/Contacts/Contacts.yml b/apidoc/Titanium/Contacts/Contacts.yml index 3663a592bfe..5631dfea1b0 100644 --- a/apidoc/Titanium/Contacts/Contacts.yml +++ b/apidoc/Titanium/Contacts/Contacts.yml @@ -247,6 +247,21 @@ methods: type: Callback properties: + - name: includeNote + summary: A boolean value that indicates whether to fetch the notes stored in contacts or not. + description: | + This property need to be set before calling contacts APIs. + From iOS 13 or later if your app fetch note field from contact, the app must have to set key 'com.apple.developer.contacts.notes' to 'true' in entitelements section of tiapp.xml. + + com.apple.developer.contacts.notes + + + See more details at https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_contacts_notes and https://developer.apple.com/contact/request/contact-note-field. + type: Boolean + default: true + platforms: [iphone, ipad] + since: 8.2.1 + - name: CONTACTS_KIND_ORGANIZATION summary: Specifies that a contact is an organization. description: | @@ -565,4 +580,4 @@ properties: --- name: ContactsAuthorizationResponse summary: Argument passed to the callback when a request finishes successfully or erroneously. -extends: ErrorResponse \ No newline at end of file +extends: ErrorResponse diff --git a/iphone/Classes/ContactsModule.h b/iphone/Classes/ContactsModule.h index 380b9797284..7334535ac2a 100644 --- a/iphone/Classes/ContactsModule.h +++ b/iphone/Classes/ContactsModule.h @@ -19,6 +19,7 @@ @private BOOL reloadAddressBook; BOOL animated; + BOOL _includeNote; KrollCallback *cancelCallback; KrollCallback *selectedPersonCallback; KrollCallback *selectedPropertyCallback; diff --git a/iphone/Classes/ContactsModule.m b/iphone/Classes/ContactsModule.m index 81ed81644c2..e246cbecac9 100644 --- a/iphone/Classes/ContactsModule.m +++ b/iphone/Classes/ContactsModule.m @@ -60,6 +60,7 @@ - (void)startup { [super startup]; contactStore = NULL; + _includeNote = YES; } //used for fetch predicates. @@ -103,6 +104,11 @@ - (NSString *)apiName #pragma mark Public API +- (void)setIncludeNote:(id)arg +{ + _includeNote = [TiUtils boolValue:arg def:YES]; +} + - (NSNumber *)hasContactsPermissions:(id)unused { NSString *calendarPermission = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSContactsUsageDescription"]; @@ -271,7 +277,11 @@ - (TiContactsPerson *)getPersonByIdentifier:(id)arg } NSError *error = nil; CNContact *contact = nil; - contact = [ourContactStore unifiedContactWithIdentifier:arg keysToFetch:[ContactsModule contactKeysWithImage] error:&error]; + NSMutableArray *contactKeys = [NSMutableArray arrayWithArray:[ContactsModule contactKeysWithImage]]; + if (!_includeNote) { + [contactKeys removeObject:CNContactNoteKey]; + } + contact = [ourContactStore unifiedContactWithIdentifier:arg keysToFetch:contactKeys error:&error]; if (error) { return nil; } @@ -327,8 +337,12 @@ - (NSArray *)getPeopleWithName:(id)arg } NSError *error = nil; NSArray *contacts = nil; + NSMutableArray *contactKeys = [NSMutableArray arrayWithArray:[ContactsModule contactKeysWithImage]]; + if (!_includeNote) { + [contactKeys removeObject:CNContactNoteKey]; + } //returns empty array or nil if there's an error - contacts = [ourContactStore unifiedContactsMatchingPredicate:[CNContact predicateForContactsMatchingName:arg] keysToFetch:[ContactsModule contactKeysWithImage] error:&error]; + contacts = [ourContactStore unifiedContactsMatchingPredicate:[CNContact predicateForContactsMatchingName:arg] keysToFetch:contactKeys error:&error]; if (!contacts) { return nil; } @@ -365,7 +379,11 @@ - (NSArray *)getAllPeople:(id)unused NSMutableArray *peopleRefs = nil; peopleRefs = [[NSMutableArray alloc] init]; //this fetch request takes all information. Not advised to use this method if addressbook is huge. May result in performance issues. - CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:[ContactsModule contactKeysWithImage]]; + NSMutableArray *array = [NSMutableArray arrayWithArray:[ContactsModule contactKeysWithImage]]; + if (!_includeNote) { + [array removeObject:CNContactNoteKey]; + } + CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:array]; BOOL success = [ourContactStore enumerateContactsWithFetchRequest:fetchRequest error:&error usingBlock:^(CNContact *__nonnull contact, BOOL *__nonnull stop) {