ios - 调用套件识别

标签 ios callkit

首先我们从服务器接收 160K 数据 然后我们尝试将这 160k 数据直接插入 CallKit,但我们收到此错误:

com.apple.CallKit.error.calldirectorymanager Code=2
CXErrorCodeCallDirectoryManagerErrorLoadingInterrupted

然后我们尝试插入30K数据,这次成功了。

我们的问题是我们不能插入超过30K,因此,我们不能使用剩余的120K。

我们如何解决这个问题?

这是我的扩展代码:

@interface CallDirectoryHandler () <CXCallDirectoryExtensionContextDelegate>
@end

@implementation CallDirectoryHandler

- (void)beginRequestWithExtensionContext:(CXCallDirectoryExtensionContext *)context {
    context.delegate = self;

    if (![self addIdentificationPhoneNumbersToContext:context]) {
        NSError *error = [NSError errorWithDomain:@"CallDirectoryHandler" code:2 userInfo:nil];
        [context cancelRequestWithError:error];
        return;
    }

    [context completeRequestWithCompletionHandler:nil];
 }

- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context {

    NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:SharedUserDefaultsSuiteName];
    containerURL = [containerURL URLByAppendingPathComponent:callDirectoryPathComponent];

    NSURL *containerURLForCallerIDFounded = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:SharedUserDefaultsSuiteName];
    containerURLForCallerIDFounded = [containerURLForCallerIDFounded URLByAppendingPathComponent:callDirectorySolvedCallerIDPathComponent];

    NSMutableDictionary *dictFromFile1 = [NSMutableDictionary dictionaryWithContentsOfFile:containerURL.path];
    NSMutableDictionary *dictFromFile2 = [NSMutableDictionary dictionaryWithContentsOfFile:containerURLForCallerIDFounded.path];
    if (dictFromFile2.count > 0) {
        [dictFromFile1 addEntriesFromDictionary:dictFromFile2];
    }
    if (dictFromFile1.count == 0) {
        return YES;
    }

    NSArray *sortedArray = [[dictFromFile1 allKeys] sortedArrayUsingComparator: ^NSComparisonResult(
                                                                                       NSString *string1,
                                                                                       NSString *string2) {
        return [string1 compare: string2 options: NSNumericSearch];
    }];

    for (int i=0; i<sortedArray.count; i++) {
        @autoreleasepool {
            NSString *number = [sortedArray objectAtIndex:i];
            NSString *name = dictFromFile1[number];
            if (number && [number isKindOfClass:[NSString class]] &&
                name && [name isKindOfClass:[NSString class]]) {
                CXCallDirectoryPhoneNumber phoneNumber = [number longLongValue];
                [context addIdentificationEntryWithNextSequentialPhoneNumber:phoneNumber label:name];
            }
            number = nil;
            name = nil;
        }
    }
    return YES;
}

#pragma mark - CXCallDirectoryExtensionContextDelegate

- (void)requestFailedForExtensionContext:(CXCallDirectoryExtensionContext *)extensionContext withError:(NSError *)error {
}

@end

最佳答案

我建议您使用@autoreleasepool将加载分成 block ,以减少内存消耗;扩展程序的内存配额比应用程序低,如果超过则将被终止。

类似于:

int offset = 0

int blockSize = 100

while offset < sortedArray.count {
    @autoreleasepool {
        for (int i=offset; i < min(sortedArray.count, offset+blockSize); i++) {
            NSString *number = [sortedArray objectAtIndex:i];
            NSString *name = dictFromFile1[number];
            if (number && [number isKindOfClass:[NSString class]] &&
            name && [name isKindOfClass:[NSString class]]) {
                CXCallDirectoryPhoneNumber phoneNumber = [number longLongValue];
                [context addIdentificationEntryWithNextSequentialPhoneNumber:phoneNumber label:name];
            }
            number = nil;
            name = nil;
        }
        offset += blockSize
    }
}

关于ios - 调用套件识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58432670/

相关文章:

ios - CXProviderDelegate 方法未触发

ios - 我可以使用 Callkit SDK 来设置对 SIP URI 的调用吗

android - 如何获取有关电话的信息

ios - 如何从我的自定义正在进行的调用 UI 结束 callkit 上的调用 session ?

ios - 在没有 Segue 的情况下实例化标签栏 Controller

ios - iPhone 上电子邮件签名中的图像

ios - Xcode 机器人服务器持续集成失败

ios - 如何在 Visual Studio for Xamarin Forms 中更改 AppId

cocoa-touch - 键盘辞职降低了弹出窗口的高度

c# - 如何使用 Xamarin 中的 CallDirectoryExtension 创建一个提供来电显示的应用程序?