【问题标题】:How to get Birthday and Anniversary labels from Contact Framework in iOS- Objective-c如何从 iOS 中的联系人框架中获取生日和周年纪念标签-Objective-c
【发布时间】:2016-10-19 01:28:01
【问题描述】:

这个问题可能有重复

为了得到这些结果,我做了一些研发。但是,目前我为此使用 XCode 7.3,我只使用 Contact Framework 而不是 ABAddressBook 框架。

使用 AddressBook Framework 获取周年纪念标签:Anniversary from contacts in iPhone

如何使用 Contacts Framework 获得这些结果以及生日列表如何?

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    添加此键以在获取联系人时获取生日:-

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

    【讨论】:

    • ThanQ @Vizllx,周年纪念标签呢?
    【解决方案2】:

    CNContact 的周年纪念日没有自己的密钥。如果存在,它将作为日期组件值通过CNContactDatesKey 驻留在联系人的[CNLabeledValue<NSDateComponents>] 数组中。你必须自己提取它。

    这是一个获取它的简短示例:

    斯威夫特 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
    }
    

    【讨论】:

      【解决方案3】:
      - (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];
          }];
      }
      
      }
      

      您可以根据要求执行类似更改密钥的操作。希望这会有所帮助:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多