2016-04-20 5 views
1

Es bekommen könnte Vervielfältigung für diese FrageWie Geburtstag und Jahrestag Etiketten von Kontakt-Framework in iOS- Objective-c

ich einig R & D tat sein, um diese Ergebnisse zu bekommen. Aber momentan verwende ich XCode 7.3 für diesen Zweck, ich benutze Contact Framework nur nicht ABAddressBook Framework.

Für Anniversary Etikett bekommen Address Framework: Anniversary from contacts in iPhone

wie kann ich diese Ergebnisse mit Hilfe Kontakte Rahmen erhalten und wie über Geburtstage Liste?

Antwort

1

diese Schlüssel hinzufügen für Geburtstage bekommen, während Kontakte zu holen: -

let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactBirthdayKey] 
+0

ThanQ @Vizllx, Was Anniversary Label? –

-1
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 


self.groupOfContacts = [@[] mutableCopy]; 
[self getAllContact]; 

self.phoneNumberArr = [@[] mutableCopy]; 

for (CNContact *contact in self.groupOfContacts) { 

    NSArray *thisOne = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"]; 


    // like this way you can pick different info like b'day and anniversary 
    [self.phoneNumberArr addObjectsFromArray:thisOne]; 
} 

NSLog(@"%@",self.phoneNumberArr); 
NSLog(@"%@",self.groupOfContacts[0]); 
// Override point for customization after application launch. 
return YES; 
} 

-(void)getAllContact{ 


if ([CNContactStore class]) { 


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

    NSArray *keysToFetch = @[CNContactBirthdayKey,CNContactNamePrefixKey,CNContactPhoneNumbersKey,CNContactFamilyNameKey,CNContactRelationsKey,CNContactDatesKey]; 

    CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch]; 

    [addressBook enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) { 
     [self.groupOfContacts addObject:contact]; 
    }]; 
} 

} 

Sie so etwas wie diese Änderung Tasten wie pro Anforderungen tun. hoffe das hilft :)

1

Ein CNContact 's Jahrestag hat keinen eigenen Schlüssel. Wenn es existiert, befindet es sich als ein Datumskomponentenwert in dem [CNLabeledValue<NSDateComponents>] Array des Kontakts über die CNContactDatesKey. Sie müssen es selbst extrahieren.

Hier ist ein kurzes Beispiel, es zu greifen:

Swift 3:

let store = CNContactStore() 
let keys = [CNContactDatesKey as CNKeyDescriptor] 
let containerId = store.defaultContainerIdentifier() 
let predicate = CNContact.predicateForContactsInContainer(withIdentifier: containerId) 

func fetchContacts() -> [CNContact]? { 
    do { 
     return try store.unifiedContacts(matching: predicate, keysToFetch: keys) 
    } catch let error { 
     print(error.localizedDescription) 
     return nil 
    } 
} 

let contacts = fetchContacts() 

// Grab the first contact as an example 
if let contact = contacts?.first { 
    // Filter through the contact's dates array to find the one labeled "Anniversary" 
    let anniversary = contact.dates.filter { date -> Bool in 
     guard let label = date.label else { return false } 
     return label.contains("Anniversary") 
     }.first?.value as? DateComponents 

    // The final date 
    let date = anniversary?.date 
} 
Verwandte Themen