1

I am building an app using objective-c. when I press button that linked to func that should open the contact of the device I get the following error and the app crashes:

plugin com.apple.MobileAddressBook.ContactsViewService interrupted Hub connection error Error Domain=NSCocoaErrorDomain Code=4097 "The operation couldn’t be completed.... uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

It's important to say that it is not always happen.. sometimes the contacts open and sometimes I get the above error.

here is my code to open the contacts:

- (IBAction)addContacts:(id)sender {
    self.user = [[User alloc] init];
    NSLog(@"ADD CONTACT");
    _addressBookController = [[ABPeoplePickerNavigationController alloc] init];
    [_addressBookController setPeoplePickerDelegate:self];
    [self presentViewController:_addressBookController animated:YES completion:nil];
}

can someone PLEASE point me to the problem?

I add the peoplePickerNavigationController:

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person{
    NSLog(@"PEOPLE PICKER");
    CFTypeRef generalCFObject = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    if (generalCFObject) {
        self.user.firstName = (__bridge NSString *)generalCFObject;
        CFRelease(generalCFObject);
    }

    generalCFObject = ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (generalCFObject) {
        self.user.lastName = (__bridge NSString *)generalCFObject;
        CFRelease(generalCFObject);
    }

    ABMultiValueRef phonesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
    for (int i=0; i<ABMultiValueGetCount(phonesRef); i++) {
        CFStringRef currentPhoneLabel = ABMultiValueCopyLabelAtIndex(phonesRef, i);
        CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, i);

        if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
            self.user.phoneNumber = (__bridge NSString *)currentPhoneValue;
        }

        CFRelease(currentPhoneLabel);
        CFRelease(currentPhoneValue);
    }

    CFRelease(phonesRef);

    [self.assignment.usersList addObject:self.user];
    [self.tableViewCell reloadData];
    [_addressBookController dismissViewControllerAnimated:YES completion:nil];

}

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    return NO;
}
//enable contact cancel buttopn
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
    [_addressBookController dismissViewControllerAnimated:YES completion:nil];
}
OriBr
  • 324
  • 2
  • 13
  • As is, your code seems fine. Show us your `ABPeoplePickerNavigationControllerDelegate` methods. Might also help to give us the stack dump at the time the exception is thrown. – David Berry Feb 18 '15 at 20:22
  • @David I re-edit my question and added the ABPeoplePickerNavigationController – OriBr Feb 18 '15 at 20:29
  • First guess, you have folks in your address book for whom you have no phone numbers. In that case, `phonesRef` will be nil, so iterating over it (and calling `CFRelease` on it, will fail. Protect usage of phonesRef as you have the other `ABRecordCopyValue` results. – David Berry Feb 18 '15 at 20:37
  • Short of that, there's still some relevant missing code (`Users` class, `assignment` – David Berry Feb 18 '15 at 20:39
  • @David when the app crashes I dont even enter the peoplePicker didSelectPerson func (I tried nslog on the begining of thr finc). so I don't believe this is the problem – OriBr Feb 18 '15 at 20:41
  • Not a clue then, as is, it's not reproducible. Have you tried restarting the device? Are you using device or simulator? – David Berry Feb 18 '15 at 20:45
  • @David I am using simulator – OriBr Feb 18 '15 at 21:14
  • Simulator support for Contacts is somewhat problematic, try it on a real device. – David Berry Feb 18 '15 at 21:15
  • Check out this answer: http://stackoverflow.com/a/32838772/3099062 – Deepak Sep 29 '15 at 08:21

0 Answers0