2016-02-05 48 views
0

我試圖實現一個聯繫人應用程序。NSOperationQueue ....無法識別的選擇器發送

在MyContactsViewController中,我試圖訪問聯繫人,如果授予訪問權限,我將從我的通訊錄中獲取聯繫人。 ContactHandler是我的模型類(單例),它有一個名爲getAllContacts的方法來獲取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; 
} 

但我得到這個錯誤 -

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

任何人都可以請幫助。

+0

'[隊列performSelectorOnMainThread:@selector (getAccessToAddressBook)withObject:self waitUntilDone:YES];'你想要做什麼?這是導致問題的線路。 – Larme

+0

通過按cmd +單擊,它是否轉到該方法?另請嘗試等待完成:否 – Hima

+0

我正在嘗試獲取權限,然後才能獲取聯繫人。基本上我想在getAccessToAddressBook方法得到響應後調用getAllContacts方法。 – Natasha

回答

1

你的問題是這條線:

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

你問queueself誰擁有這個選擇何時執行getAccessToAddressBook

如果你想運行隊列中的方法getAccessToAddressBook,您可以使用- addOperationWithBlock:

0

由於在這一行:performSelectorOnMainThread你在哪裏在方法定義,即(BOOL)getAccessToAddressBook你不使用它,這是造成死機,所以請withObject:nil逝去withObject:self,如果你不想傳遞任何對象或值該方法。

改變這一行: -

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

這樣: -

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

或者更好的辦法將是這樣寫: -

NSOperationQueue *opQueue=[NSOperationQueue new]; 
    NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getAccessToAddressBook) object:nil]; 
[opQueue addOperation:op ]; 
+0

我把它改爲零,但它仍然給我同樣的錯誤。 – Natasha

相關問題