如何使用具有长联系人列表的 Apple 联系人框架更快地获取 iOS 联系人?

2024-04-27

我在用联系方式获取我的 iOS 设备中的电话簿联系人。

当我的手机中有少量联系人(例如 50 个)时,可以轻松获取联系人。

但是,当我有很多联系人(比如 500-700)时,它会挂起/等待很长时间才能将这些联系人从 iOS 电话簿获取到我的应用程序的数组中。

以前我用过https://github.com/Alterplay/APAddressBook https://github.com/Alterplay/APAddressBook这是以前Apple框架的快速库,但现在我使用最新的Apple框架。

我获取联系人的代码是......

#pragma mark - Get All Contacts....

-(void)getAllContactsWithNewFrameworkOfApple {

    NSMutableArray *_contactsArray = [[NSMutableArray alloc]init];

    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusDenied) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"This app previously was refused permissions to contacts; Please go to settings and grant permission to this app so it can use contacts" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
        //        [self presentViewController:alert animated:TRUE completion:nil];
        return;
    }

    CNContactStore *store = [[CNContactStore alloc] init];

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

        // make sure the user granted us access

        if (!granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                // user didn't grant access;
                // so, again, tell user here why app needs permissions in order  to do it's job;
                // this is dispatched to the main queue because this request could be running on background thread
            });
            return;
        }

        NSError *fetchError;
        CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey,CNContactGivenNameKey,CNContactFamilyNameKey,CNContactEmailAddressesKey,CNContactPhoneNumbersKey]];

        BOOL success = [store enumerateContactsWithFetchRequest:request error:&fetchError usingBlock:^(CNContact *contact, BOOL *stop) {

            [_contactsArray addObject:contact];
            NSLog(@"I am %@", _contactsArray);

        }];

        if (!success) {
            NSLog(@"error = %@", fetchError);
        }else {

                        NSLog(@"End with Success %@", _contactsArray);
            [self finalUsage:_contactsArray];

        }

    }];



}

我试图批量获取 20 个联系人,每次都重新加载表。然而,这里它无法在调度线程内重新加载,或者只是崩溃。

如何改进此代码以快速获取联系人?

Thanks.


正如托罗提到的,requestAccessForEntityType: completionHandler:方法未在主 (UI) 线程上运行。来自文档 https://developer.apple.com/library/prerelease/ios/documentation/Contacts/Reference/CNContactStore_Class/index.html#//apple_ref/occ/instm/CNContactStore/requestAccessForEntityType:completionHandler:你可以看到它提到:

用户可以授予或拒绝对联系人数据的访问 每个应用程序的基础。通过致电请求访问联系数据 requestAccessForEntityType:completionHandler: 方法。这不会 在请求用户许可时阻止您的应用程序。 仅在第一次请求访问时才会提示用户;任何 后续 CNContactStore 调用将使用现有权限。时间他 在任意队列上调用完成处理程序。推荐 您在此完成中使用 CNContactStore 实例方法 处理程序而不是 UI 主线程。当以下情况时,此方法是可选的 CNContactStore 在后台线程中使用。如果这个方法不是 使用时,CNContactStore 可能会在用户使用时阻止您的应用程序 请求访问权限。

因此,您想要在 UI 中执行的任何更新都必须在主线程上执行。

dispatch_async(dispatch_get_main_queue(), ^{
   //Update UI on the Main thread, like reloading a UITableView
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用具有长联系人列表的 Apple 联系人框架更快地获取 iOS 联系人? 的相关文章

随机推荐