iOS CallKit呼叫识别

时间:2018-03-12 15:54:30

标签: ios swift callkit

我已成功安装了扩展程序 CXCallDirectoryProvider ,以便识别预先添加的电话条目。我在网上找到的文件相当不足。 尽管如此,我已经设法达到了我最初导入必要的电话条目。 通过覆盖 func beginRequest(使用上下文: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)
    }

}

以上代码在用户到达设置时运行 - &gt;电话 - &gt;呼叫阻止&amp;识别并启用应用名称旁边的句柄。

现在问题部分......

此代码如何阻止

if context.isIncremental {
            addOrRemoveIncrementalBlockingPhoneNumbers(to: context)
            addOrRemoveIncrementalIdentificationPhoneNumbers(to: context)
        }

触发以更新经常更改的记录?

是否有任何指导方针/模式或几乎任何可以指向正确方向的关于如何维护这些条目并在更改时保持最新,或添加新的,甚至删除过时的条目。我在网上找不到关于增量的任何内容,仅用于初始 addAllIdentificationPhoneNumbers

1 个答案:

答案 0 :(得分:0)

只有在您的分机运行一次并提供其初始的完整基准数据集后,才会请求增量数据集。

例如,如果您的扩展程序在安装后运行一次,提供其基准数据集,然后您的应用程序调用{​​{1}}以请求重新运行扩展程序,则后续重新运行应为增量运行。

相关问题