Search code examples
iosobjective-ccontactsaddressbooknsoperationqueue

NSOperationQueue ....unrecognized selector sent


I am trying to implement a contact app.

In the MyContactsViewController, I am trying to get access to Contacts and if the access is granted I will fetch the contacts from my address book. The ContactHandler is my model class(singleton) which has the method called getAllContacts to get the contacts in a NSMutableArray.

- (void)viewDidLoad {
    [super viewDidLoad];
    contactHandler = [ContactHandler sharedInstance];
    if(!self.accessGranted){
        NSOperationQueue *queue =[[ NSOperationQueue alloc]init];
        [queue performSelectorOnMainThread:@selector(getAccessToAddressBook) withObject:self waitUntilDone:YES];
        contactList = [contactHandler getAllContacts];
    }
    else{
        contactList = [contactHandler getAllContacts];
    }

}

-(BOOL)getAccessToAddressBook{

    CNContactStore * contactStore = [[CNContactStore alloc] init];

    if( [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined){

        [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if(granted){
                self.accessGranted = YES;
            }
            else{
                self.accessGranted = NO;
            }
        }];
    }
    else if( [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]== CNAuthorizationStatusAuthorized){
        self.accessGranted = YES;
    }
    else{
        self.accessGranted = NO;
    }
    return self.accessGranted;
}

But I am getting this error -

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSOperationQueue getAccessToAddressBook]: unrecognized selector sent to instance 0x137630700'

Can anyone please help.


Solution

  • Your problem is this line:

    [queue performSelectorOnMainThread:@selector(getAccessToAddressBook) withObject:self waitUntilDone:YES];
    

    You're asking queue to perform getAccessToAddressBook when is self who has this selector

    If you want to run the method getAccessToAddressBook on the queue, you can use - addOperationWithBlock: