【问题标题】:CNContact Frame work not fetching all contacts(iCloud,Gmail)CNContact 框架未获取所有联系人(iCloud,Gmail)
【发布时间】:2017-05-25 06:29:24
【问题描述】:

设置>联系人>默认帐户选择为 iCloud 并保存了一个新联系人。然后将默认帐户更改为 gmail。 CNContactStore 未获取新保存的联系人。

 CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted == YES)
    {
        NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey,CNContactEmailAddressesKey];
        NSArray * contactContainerArray =  [store containersMatchingPredicate:nil error:nil];
        for(CNContainer * container in contactContainerArray) {
            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:container.identifier];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
}

【问题讨论】:

    标签: ios objective-c iphone contacts


    【解决方案1】:

    如果您只是希望获得所有统一联系人(代表同一个人的不同帐户中的联系人可能会自动链接在一起。链接的联系人在 macOS 和 iOS 应用程序中显示为统一联系人)最好的解决方案是低于一个

     -(void)fetchContacts
      {
          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,CNContactEmailAddressesKey];
            NSString *containerId = store.defaultContainerIdentifier;
            NSArray * contactContainerArray =  [store containersMatchingPredicate:nil error:nil];
            CNContactFetchRequest * fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keys];
            [store enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
    
            }];
          }
      }
    

    【讨论】:

    • 伟大的解决方案 Vaisakh。另外能否请您告诉我如何识别联系人属于哪个“帐户”?表示如何识别特定联系人来自 iCloud 或 Gmail 或本地通讯录?
    • 我已经在这里问了我的问题stackoverflow.com/questions/44133669/…
    • @MitBhatt 实际问题将为您提供容器中的所有联系人(用户可能在其设备的本地帐户或配置为同步联系人的服务器帐户中有联系人。每个帐户至少有一个联系人容器。一个联系人只能在一个容器中。)
    • 知道了,但是如何获取该容器的名称?
    • @MitBhatt 检查stackoverflow.com/questions/44133669/…这个链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多