ios - 使用新的联系人框架获取所有联系人和所有 key

标签 ios swift xcode8 contacts

我想获取所有没有过滤器的联系人及其 key 的所有信息。

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

我正在使用上述方法来获取联系人。

我必须指定所有键吗? 他们是否有任何快捷方式可以在不指定所有 key 的情况下获取所有 key 信息?

最佳答案

没有。您必须自己指定所有必需的 key 。请查看苹果文档here **编辑:**请尝试此方法:

-(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];
        }
    }
}

关于ios - 使用新的联系人框架获取所有联系人和所有 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450643/

相关文章:

swift - NoOptionalInterpolation Pod 错误 Swift

ios - 使用未解析的标识符 userDefaults

ios - Swift - 自定义 UIImageView 类

swift - 尝试将图像保存到照片库时 iOS 10 应用程序崩溃

ios - CKAsset (NSData) 到 UIWebView 与 ActivityViewController 共享

objective-c - 使用 Objective-C 在 iOS 警报消息中添加换行符

ios - 零检查 block

ios - 如何解决此错误,例如 Invalid `Podfile` file : [! ] Unsupported options `{:exclusive=>true}` for target

iphone - cocos2d 中的自由运行/横向滚动类型游戏

ios - Realm swift : Database won't be updated after backup