【问题标题】:Fetch all the contact and all the keys using new Contact Framework使用新的联系人框架获取所有联系人和所有密钥
【发布时间】:2016-07-19 06:01:21
【问题描述】:

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

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

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

我必须指定所有的键吗? 它们是获取所有键信息而不指定所有键信息的快捷方式吗?

【问题讨论】:

    标签: ios swift xcode8 contact


    【解决方案1】:

    没有。您必须自己指定所有必需的键。请查看苹果文档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];
            }
        }
    }
    

    【讨论】:

    • 那么谓词..我必须指定它吗?因为我想要所有的联系人。
    • 如果要过滤数据,可以使用谓词。像 let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName("XYZ"), keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey]) 这将返回与名称“XYZ”匹配的联系人,键将是 CNContactGivenNameKey 和 CNContactFamilyNameKey。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多