联系框架只处理了几个联系人



我正在使用联系人框架将所有联系人从我的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];
     }

试试这个:

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

最新更新