iOS CallKit 通话识别

3

我已成功安装了一个扩展程序 CXCallDirectoryProvider,以识别预先添加的电话条目。我在网上找到的文档相当不足。 尽管如此,我已经成功地完成了必要电话条目的初始导入。 通过覆盖 func beginRequest(with context: CXCallDirectoryExtensionContext) 函数。 更具体地说:

override func beginRequest(with context: CXCallDirectoryExtensionContext) {
    context.delegate = self

    // Check whether this is an "incremental" data request. If so, only provide the set of phone number blocking
    // and identification entries which have been added or removed since the last time this extension's data was loaded.
    // But the extension must still be prepared to provide the full set of data at any time, so add all blocking
    // and identification phone numbers if the request is not incremental.

    if #available(iOS 11, *) {
        if context.isIncremental {
            addOrRemoveIncrementalBlockingPhoneNumbers(to: context)
            addOrRemoveIncrementalIdentificationPhoneNumbers(to: context)
        } else {
            addAllBlockingPhoneNumbers(to: context)
            addAllIdentificationPhoneNumbers(to: context)
        }
    } else {
        addAllBlockingPhoneNumbers(to: context)
        addAllIdentificationPhoneNumbers(to: context)
    }

    context.completeRequest()
}

上述代码中所调用的函数最初安装电话记录的功能,称为addAllIdentificationPhoneNumbers,非常顺畅地运行。
private func addAllIdentificationPhoneNumbers(to context: CXCallDirectoryExtensionContext) {
    // Retrieve phone numbers to identify and their identification labels from data store. For optimal performance and memory usage when there are many phone numbers,
    // consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded.
    //
    // Numbers must be provided in numerically ascending order.


    var allNumbers = [(Number: String, Fullname: String)]()

    // load allNumbers from local db 
    // ...
    // ...
    // ...

    var test = allNumbers.map { s -> (Number: CXCallDirectoryPhoneNumber, Fullname:String) in
        (CXCallDirectoryPhoneNumber(s.Number)!, s.Fullname)
    }

    // inc order 
    test.sort(by: { $0.Number < $1.Number })

    // do the mapping to satisfy CallKit
    let allPhoneNumbers = test.map { $0.Number }

    let labels = test.map { $0.Fullname }

    for (phoneNumber, label) in zip(allPhoneNumbers, labels) {
        context.addIdentificationEntry(withNextSequentialPhoneNumber: phoneNumber, label: label)
    }

}

当用户到达“设置” -> “电话” -> “呼叫阻止和识别”并启用应用程序名称旁边的处理程序时,以上代码将运行。 现在是问题部分... 这段代码如何阻止呢?
if context.isIncremental {
            addOrRemoveIncrementalBlockingPhoneNumbers(to: context)
            addOrRemoveIncrementalIdentificationPhoneNumbers(to: context)
        }

如何触发更新经常更改的记录?

是否有任何指导方针/模式或任何可以指引我正确方向的东西,以维护这些条目并在其更改、添加新条目甚至删除过时条目时保持其最新状态。我在网上找不到关于增量的任何内容,只有关于初始addAllIdentificationPhoneNumbers的信息。

1个回答

1

仅在您的扩展运行一次并提供其初始完整基线数据后,将请求增量数据集。

例如,如果您的扩展程序在安装后运行一次,提供其基线数据集,然后您的应用程序调用CXCallDirectoryManager.reloadExtension(identifier:completion:)来请求重新运行扩展程序,则随后的重新运行应该是增量的。


那么答案是,这取决于操作系统是否调用.reloadExtension()? 这意味着当操作系统决定运行扩展程序时,附加调试器将会运行?有没有办法手动触发?如何调试此函数?这就是我一开始的问题... - Jimi
reloadExtension()通常在您的应用程序有新数据需要加载时由应用程序调用,但也会在用户在设置中启用应用程序扩展的第一次调用。如果您想进行调试,可以首先使用“等待启动”选项在Xcode中附加,然后在设置中启用扩展。 - Stuart M
1
我的问题中Stuart已经很清楚地表明了,即当用户启用扩展时的情况是被充分理解和简单明了的。我第三次提问的是,当你的应用程序有新数据需要加载时,通常何时会调用__reloadExtension()__?应用程序如何__判断__是否有新数据需要加载?我该如何提供这些新数据? - Jimi
1
每当您的应用程序收到新数据时,它会调用reloadExtension()方法。通过使用iOS的后台刷新、静默推送通知或用户手动更新等功能,应用程序可以了解到新数据。 - Stuart M

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接