App crashes in iOS 13 when pushing ABPersonViewController

329 Views Asked by At

Till iOS 12 code is working fine. But the below code is crashing the application on iOS13.

NOTE: I do not want to upgrade to Contacts framework right now.

Sharing my code below:

ABPersonViewController *vc = [[ABPersonViewController alloc] init];
vc.displayedPerson = person;
vc.addressBook = parser.addressbook;
vc.allowsEditing = YES;
vc.personViewDelegate = self;
[self.navigationController pushViewController:vc animated:YES];

When pushing, app crashes only on iOS 13. sharing below crash stack:

*** Terminating app due to uncaught exception 'CNPropertyNotFetchedException', reason: 'Contact 0x7fb020d2bc70 is missing some of the required key descriptors: (
    "<CNAggregateKeyDescriptor: 0x600003176be0: kind=+[CNContactContentViewController descriptorForRequiredKeys]>"
)'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e63127e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x000000010deb3b20 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e6310bc +[NSException raise:format:] + 188
    3   Contacts                            0x000000010e03f0fb -[CNContact assertKeysAreAvailable:] + 119
    4   ContactsUI                          0x000000012a2ded30 +[CNContactViewController viewControllerForContact:] + 137
    5   AddressBookUI                       0x000000010df62233 -[ABPersonViewController reloadContactViewController] + 858
    6   UIKitCore                           0x0000000113361aca -[UIViewController loadViewIfRequired] + 172
    7   UIKitCore                           0x0000000113362277 -[UIViewController view] + 27
    8   UIKitCore                           0x00000001132b13dd -[UINavigationController _startCustomTransition:] + 1039
    9   UIKitCore                           0x00000001132c730c -[UINavigationController _startDeferredTransitionIfNeeded:] + 698
    10  UIKitCore                           0x00000001132c8721 -[UINavigationController __viewWillLayoutSubviews] + 150
    11  UIKitCore                           0x00000001132a9553 -[UILayoutContainerView layoutSubviews] + 217
    12  UIKitCore                           0x0000000113ec64bd -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
    13  QuartzCore                          0x000000010ee87db1 -[CALayer layoutSublayers] + 255
    14  QuartzCore                          0x000000010ee8dfa3 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
    15  QuartzCore                          0x000000010ee998da _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
    16  QuartzCore                          0x000000010ede0848 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
    17  QuartzCore                          0x000000010ee15b51 _ZN2CA11Transaction6commitEv + 643
    18  UIKitCore                           0x0000000113a0a3f4 _afterCACommitHandler + 160
    19  CoreFoundation                      0x000000010e593867 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    20  CoreFoundation                      0x000000010e58e2fe __CFRunLoopDoObservers + 430
    21  CoreFoundation                      0x000000010e58e97a __CFRunLoopRun + 1514
    22  CoreFoundation                      0x000000010e58e066 CFRunLoopRunSpecific + 438
    23  GraphicsServices                    0x0000000117be9bb0 GSEventRunModal + 65
    24  UIKitCore                           0x00000001139e0d4d UIApplicationMain + 1621
    25  QA cleanup dupes                    0x000000010bf5ace0 main + 112
    26  libdyld.dylib                       0x000000010fb72c25 start + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I'm using AddressBook framework. Please help me get through this. Thanks

1

There are 1 best solutions below

0
Jawad Ali On

AddressBook and AddressBookUI Is deprecated

Better is to shift to

 @import Contacts; 
 @import ContactsUI;


-(void)fetchAllContacts:(void(^)(BOOL success, NSError * _Nullable error))completion
{
    self.contactArray = [[NSMutableArray alloc] init];

    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

        if (success)
        {
            NSArray *keys = @[CNContactNamePrefixKey, CNContactFamilyNameKey, CNContactGivenNameKey,
                              CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactEmailAddressesKey];

            NSString *containerId = store.defaultContainerIdentifier;
            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];

            if (error)
            {
                NSLog(@"ERROR IN FETCHING: %@", error.description);
            }
            else
            {
                for (CNContact *contact in cnContacts)
                {
                    @try
                    {
                        CMCustomContacts *newContact = [[CMCustomContacts alloc] init];
                        newContact.phoneArray = [[NSMutableArray alloc] init];
                        newContact.emailArray = [[NSMutableArray alloc] init];

                        newContact.firstName = contact.givenName;
                        newContact.lastName = contact.familyName;

                        UIImage *image = [UIImage imageWithData:contact.imageData];
                        newContact.profileImage = image;

                        for (CNLabeledValue *label in contact.phoneNumbers)
                        {
                            NSString *phone = [label.value stringValue];
                            if ([phone length] > 0)
                            {
                                [newContact.phoneArray addObject:phone]; NSLog(@"PHONE NUMBER: %@",phone);
                            }
                        }

                        for (CNLabeledValue *label in contact.emailAddresses)
                        {
                            NSString *email = label.value;
                            if ([email length] > 0)
                            {
                                [newContact.emailArray addObject:email];  NSLog(@"EMAIL :: %@",email);
                            }
                        }

                        [self.contactArray addObject:newContact];
                    }
                    @catch (NSException *exception)
                    {
                        NSLog(@"EXCEPTION IN CONTACTS :: %@", exception.description);
                    }
                    @finally
                    {
                        NSLog(@"FINALLY");
                    }
                }

                NSLog(@"COUNT OF CONTACTS :: %lu", (unsigned long)self.contactArray.count);
            }
        }
        completion(granted, error);
    }];