2016-11-07 131 views
0

由於某些原因,我的一些聯繫人在我的tableView中缺失。我相信我會提取所有必要的密鑰並審查了Apple的文檔,但我仍然陷入困境。以下是我的代碼:聯繫人框架中缺少聯繫人(Objective-C)

-(void)fetchContactsandAuthorization { 
// Request authorization to Contacts 
CNContactStore *store = [[CNContactStore alloc] init]; 
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { 
    if (granted == YES) 
    { 

     //keys with fetching properties 
     NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]; 
     NSString *containerId = store.defaultContainerIdentifier; 
     NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId]; 
     NSError *error; 
     NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; 
     if (error) 
     { 
      NSLog(@"error fetching contacts %@", error); 
     } 
     else 
     { 
      NSString *phone; 
      NSString *fullName; 
      NSString *firstName; 
      NSString *lastName; 
      UIImage *profileImage; 
      NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init]; 
      for (CNContact *contact in cnContacts) { 
       // copy data to my custom Contacts class. 
       firstName = contact.givenName; 
       lastName = contact.familyName; 
       if (lastName == nil) { 
        fullName=[NSString stringWithFormat:@"%@",firstName]; 
       }else if (firstName == nil){ 
        fullName=[NSString stringWithFormat:@"%@",lastName]; 
       } 
       else{ 
        fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName]; 
       } 

       UIImage *image = [UIImage imageWithData:contact.imageData]; 
       if (image != nil) { 
        profileImage = image; 
       }else{ 
        profileImage = [UIImage imageNamed:@"person-icon.png"]; 
       } 

       NSString *number = contact.phoneNumbers.firstObject.value.stringValue; 

       if (number == (NSString *)NSNull.null || number == nil) { 
        [contactNumbersArray addObject:@"NO NUMBER"]; 
       } else { 
        [contactNumbersArray addObject:number]; 
       } 


       arrayPhoneData = contactNumbersArray; 
       NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil]; 
       [arrayTableData addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"fullName"]]]; 
      } 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       //[tableViewContactData reloadData]; 
       [self.tableView reloadData]; 
      }); 
     } 
     } 
    }]; 
} 

任何幫助非常感謝!

+0

張貼失蹤和非接觸缺失,包括包含組,比較 – shallowThought

回答

0
` 
let keys = [CNContactGivenNameKey, CNContactPhoneNumbersKey] as [Any] 

     do { 
      let contactStore = AppDelegate.getAppDelegate().contactStore 
      try contactStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: keys as! [CNKeyDescriptor])) { (contact, pointer) -> Void in 
       if !contact.phoneNumbers.isEmpty { 
        contacts.append(contact) 
       } 
      } 
     } 
     catch let error as NSError { 

      print(error.description, separator: "", terminator: "\n") 
     } 


     let contactPickerViewController = CNContactPickerViewController() 

     contactPickerViewController.delegate = self 

     present(contactPickerViewController, animated: true, completion: nil) 
` 

棒這應用程序委託

var contactStore = CNContactStore() 

    class func getAppDelegate() -> AppDelegate { 
     return UIApplication.shared.delegate as! AppDelegate 
    } 

嘗試仔細檢查您的謂語。我在限制我一直在尋找的聯繫人的謂詞方面遇到了麻煩,並在過去完全刪除了聯繫人。不是那樣,但你可以嘗試添加更多的鍵到你的聯繫人鍵。您可能會用keysToFetch限制搜索。

+0

試圖添加多個鍵,但似乎並沒有工作。如果刪除謂詞,您是如何獲得cnContacts的? – a2b123

+0

對不起,延遲迴復 – Fallah

0

您應該以不同方式訪問聯繫簿。

dispatch_async(dispatch_get_main_queue(), ^{ 

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

    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { 

     if (granted == NO) { 
      //show access denied popup 
      return ; 
     } 


     NSError* contactError; 
     CNContactStore* addressBook = [[CNContactStore alloc]init]; 
     [addressBook containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers: @[addressBook.defaultContainerIdentifier]] error:&contactError]; 
     NSArray *keysToFetch = @[CNContactMiddleNameKey,CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey,CNContactEmailAddressesKey]; 
     CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch]; 


     [addressBook enumerateContactsWithFetchRequest:request error:&contactError usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop){\ 

      NSString *firstName = contact.givenName; 
      NSString *lastName = contact.familyName; 

      //recommend to use a function like this one: [self parseContactWithContact:contact]; 
     }]; 


    }]; 
});