OGeek|极客世界-中国程序员成长平台

标题: ios - 联系人框架只增加了几个联系人 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:38
标题: ios - 联系人框架只增加了几个联系人

我正在使用 Contacts Framework 将所有联系人从我的 iPhone 抓取到我的应用程序。但它只获取少数联系人。这是我使用此代码的代码:

//ios 9+
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted == YES) {

        NSArray *keys = @[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey,CNLabelPhoneNumberMobile,CNLabelPhoneNumberMain,CNContactImageDataKey,CNContactIdentifierKey];
        NSString *containerId = store.defaultContainerIdentifier;
        NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
        NSError *error;
        NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];

     }



Best Answer-推荐答案


试试这个:

    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

        if (granted == YES) {

            NSArray *keys = @[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey,CNLabelPhoneNumberMobile,CNLabelPhoneNumberMain,CNContactImageDataKey,CNContactIdentifierKey];
            NSError *error;
            NSArray *allContainer = [store containersMatchingPredicate:nil error:&error];

            for (CNContainer *container in allContainer) {

                    NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:container.identifier];
                    NSArray *result = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
                    [aryAllContacts addObject:result];
            }
        }
    }];

关于ios - 联系人框架只增加了几个联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43884464/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4