Waseem05
Waseem05

Reputation: 1224

ABPeoplePickerNavigationController crashes in IOS 7

I have problem with ABPeoplePickerNavigationController in IOS 7 with the following error

*** -[ABPeoplePickerNavigationController respondsToSelector:]: message sent to deallocated instance 0x9b4b050  

on IOS 6 it is working fine but in ios 7 it gives this error with zombies enabled without zombies it was like

Thread 1: EXC_BAD_ACCESS(code=2,address=0x0)    

than i enable zombies
here is my code

 - (void)viewDidLoad
 {  
       [super viewDidLoad];
       self.contacts = [[NSMutableArray alloc] initWithCapacity:10];
       self.addressBook=ABAddressBookCreateWithOptions(NULL, NULL);
      [self checkAddressBookAccess];
 }     
 (void)requestAddressBookAccess
 {  
        ContactsViewController * __weak weakSelf = self;

        ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted,                             CFErrorRef error)
                                         {
                                             if (granted)
                                             {
                                                 dispatch_async(dispatch_get_main_queue(), ^{
                                                     [weakSelf accessGrantedForAddressBook];

                                                 });
                                             }
                                         });
 }
    -(void)accessGrantedForAddressBook
    {      
       NSMutableArray *savedContacts=[[NSMutableArray alloc] initWithArray:[DatabaseHandler getAllContacts]];
        if (savedContacts &&savedContacts.count!=0) 

       [self.contacts addObjectsFromArray:savedContacts];
    }
   - (IBAction)popUpAddExistingContact:(id)sender {    

    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [picker setDelegate:self];
    [self presentViewController:picker animated:YES completion:nil];

 }
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{


NSString *viewControllerDesc=[viewController description];
NSString *t_st = @"ABContactViewController";
NSRange rang =[viewControllerDesc rangeOfString:t_st options:NSCaseInsensitiveSearch];

if (rang.length == [t_st length])
{
    navigationController.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(addPerson:)];
}
else if([navigationController isKindOfClass:[ABPeoplePickerNavigationController class]] && [viewController isKindOfClass:[ABPersonViewController class]])
{
    ABPersonViewController *DVC=(ABPersonViewController*)viewController;
    self.currentPerson=DVC.displayedPerson;
    navigationController.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(addPerson:)];

}
else{
    navigationController.topViewController.navigationItem.rightBarButtonItem = nil;
}

navigationController.topViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
}
-(IBAction)addPerson:(id)sender{

if (self.currentPerson!=NULL)
{
    CFStringRef firstName;
    int recordID;
    firstName = ABRecordCopyValue(self.currentPerson, kABPersonFirstNameProperty);
    recordID = ABRecordGetRecordID(self.currentPerson);
    MyContact *contact=[[MyContact alloc] init];
    contact.Name=(__bridge NSString *)(firstName);
    contact.contactID=[NSString stringWithFormat:@"%i",recordID];

    contact.phones=[[NSMutableArray alloc] init];
    ABMultiValueRef phones = ABRecordCopyValue(self.currentPerson, kABPersonPhoneProperty);
    for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
    {
        CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
        CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, j);
        NSString *phoneNumber = (__bridge NSString *)phoneNumberRef;

        [contact.phones addObject:phoneNumber];

        CFRelease(phoneNumberRef);
        CFRelease(locLabel);
    }


    CFRelease(firstName);
    //CFRelease(lastName);
}
//[self dismissModalViewControllerAnimated:YES];

[self dismissViewControllerAnimated:YES completion:^(void ){

    [self.popUpContactView removeFromSuperview];

}];

}
as soon as peoplepickercontroller is dismissed app crashed in ios 7

*** -[ABPeoplePickerNavigationController respondsToSelector:]: message sent to deallocated instance 0xb236c00
0x17d811:  jmp    0x17d90c                  ; ___forwarding___ + 1020
Thread 1:EXC_BREAKPOINT (code=EXC_1386_BPT,sucode 0x0)

Upvotes: 2

Views: 655

Answers (2)

Nikolay Shubenkov
Nikolay Shubenkov

Reputation: 3223

try to set peopleViewController delegates to nil before dismissing and disable second possible call of this action during some time (like if user pressed button several times). Assume you have a reference to ABPeoplePickerNavigationController instance. Something name like self.adressBook;

Then before dismissing PeoplePickerNavigationController set

self.adressBook.peoplePickerDelegate = nil
self.adressBook.delegate = nil;

and make sure that you do not call your peoplePickerNavigationController reference after dismissing or your reference to this instance is of weak, not assign type.

Upvotes: 1

Nagaraj
Nagaraj

Reputation: 802

- (IBAction)popUpAddExistingContact:(id)sender {    

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[picker setDelegate:self];
[self presentViewController:picker animated:YES completion:nil];

}

I think it is because the view controller created is not retained, Call [self addChildViewController:picker] or else maintain a "strong" reference for it

Upvotes: 0

Related Questions