2016-11-07 1 views
0

Aus irgendeinem Grund fehlen einige meiner Kontakte in meinem TableView. Ich glaube, ich hole alle notwendigen Schlüssel und habe Apples Dokumentation überprüft, bin aber immer noch festgefahren. Unten ist mein Code:Fehlende Kontakte aus dem Kontakt-Framework (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]; 
      }); 
     } 
     } 
    }]; 
} 

Jede Hilfe wird sehr geschätzt !!!!

+0

Posten fehlen würde und ein nicht-fehlenden Kontakt enthielt Gruppe einschließlich, – shallowThought

Antwort

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) 
` 

Stick dies in AppDelegate

var contactStore = CNContactStore() 

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

Versuchen Doppel Ihr Prädikat überprüfen. Ich hatte Probleme mit dem Prädikat, das die Kontakte einschränkte, nach denen ich suchte, und entfernte es in der Vergangenheit vollständig. Nicht nur das, aber Sie könnten versuchen, weitere Schlüssel zu Ihren Kontaktschlüsseln hinzuzufügen. Sie beschränken möglicherweise Ihre Suche mit keysToFetch.

+0

mehr Tasten Versuchte vergleichen hinzuzufügen, aber das scheint nicht zu funktionieren. Wie haben Sie cnContacts zum Laufen gebracht, wenn Sie das Prädikat entfernt haben? – a2b123

+0

Entschuldigung für die späte Antwort – Fallah

0

Sie sollten auf das Kontaktbuch anders zugreifen.

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]; 
     }]; 


    }]; 
}); 
Verwandte Themen