2016-07-19 28 views
0

Ich möchte alle Kontakte ohne Filter und alle Informationen ihrer Schlüssel abrufen.Holen Sie alle Kontakt und alle Schlüssel mit neuen Contact Framework

func unifiedContactsMatchingPredicate(_ predicate: NSPredicate, 
          keysToFetch keys: [CNKeyDescriptor]) throws -> [CNContact] 

Ich verwende oben genannte Methode, um Kontakt zu holen.

muss ich alle Schlüssel angeben? ist ihre Abkürzung, um alle Schlüsselinformationen abzurufen, ohne sie alle anzugeben?

Antwort

0

Nein. Sie müssen alle erforderlichen Schlüssel selbst angeben. Bitte haben Sie einen Blick auf Apple-Dokumentation here ** EDIT: ** Bitte versuchen Sie diese Methode:

-(void)getAllContacts 
{ 
    //keys with fetching properties 
    NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]; 
    NSString *containerId = store.defaultContainerIdentifier; 
    //to fetch all contacts 
    NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId]; 

    //to fetch contacts with matching name 
// NSPredicate *predicate = [CNContact predicateForContactsMatchingName:@"vishal"]; 
    NSError *error; 
    NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; 
    if (error) { 
     NSLog(@"error fetching contacts %@", error); 
    } else { 
     for (CNContact *contact in cnContacts) { 
      // copy data to my custom Contacts class. 
      Contact *newContact = [[Contact alloc] init]; 
      newContact.firstName = contact.givenName; 
      newContact.lastName = contact.familyName; 
      UIImage *image = [UIImage imageWithData:contact.imageData]; 
      newContact.image = image; 
      for (CNLabeledValue *label in contact.phoneNumbers) { 
       NSString *phone = [label.value stringValue]; 
       if ([phone length] > 0) { 
        [newContact.phones addObject:phone]; 
       } 
      } 
      [contacts addObject:newContact]; 
     } 
    } 
} 
+0

und was Prädikat ..ist i muss sie angeben müssen? coz ich will alle Kontakte. – commando24

+0

Sie können Prädikat verwenden, wenn Sie die Daten filtern möchten. Wie let Kontakte = try store.unifiedContactsMatchingPredicate (CNContact.predicateForContactsMatchingName ("XYZ"), keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey]) Dies wird Kontakte zurückgeben, die mit dem Namen "XYZ" übereinstimmen und die Schlüssel sind CNContactGivenNameKey und CNContactFamilyNameKey. –

+0

Ich weiß das, aber was ich verlange, ist "alle" Kontakte zu holen. – commando24