这个问题可能有重复
我做了一些R& D获得这些结果。但是,目前我正在使用XCode 7.3,我只使用Contact Framework而不是ABAddressBook Framework。
使用AddressBook Framework获取周年纪念标签:Anniversary from contacts in iPhone
如何使用“联系人框架”获取这些结果以及生日列表如何?
答案 0 :(得分:1)
在获取联系人时添加此密钥以获取生日: -
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactBirthdayKey]
答案 1 :(得分:1)
CNContact
周年纪念日没有自己的钥匙。如果存在,则它通过[CNLabeledValue<NSDateComponents>]
作为日期组件值驻留在联系人的CNContactDatesKey
数组中。你必须自己提取它。
以下是抓住它的简短示例:
斯威夫特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
}
答案 2 :(得分:-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];
}];
}
}
您可以根据要求执行类似此更改键的操作。希望这会有所帮助:)