首页 文章

iOS CallKit呼叫识别

提问于
浏览
0

我已成功安装了扩展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 and works like a charm .

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)
    }

}

当用户到达设置 - >电话 - >呼叫阻止和识别时,上面的代码会运行,并启用应用名称旁边的句柄 .

And now the question part...

这段代码是如何阻止的

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

get triggered in order to update records that often change?

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

2 回答

  • -3

    只有在您的扩展程序运行一次并提供其初始的完整基准数据集后,才会请求增量数据集 .

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

  • 0

    经过这么多的研究,我不得不“翻译”一些关于此事的日本参考文献并提出:

    CXCallDirectoryManager.sharedInstance.reloadExtension(withIdentifier: "bundleIdentifier") { errorOrNil in
                    if let error = errorOrNil as? CXErrorCodeCallDirectoryManagerError {
                        print("reload failed")
    
                        switch error.code {
                        case .unknown:
                            print("error is unknown")
                        case .noExtensionFound:
                            print("error is noExtensionFound")
                        case .loadingInterrupted:
                            print("error is loadingInterrupted")
                        case .entriesOutOfOrder:
                            print("error is entriesOutOfOrder")
                        case .duplicateEntries:
                            print("error is duplicateEntries")
                        case .maximumEntriesExceeded:
                            print("maximumEntriesExceeded")
                        case .extensionDisabled:
                            print("extensionDisabled")
                        case .currentlyLoading:
                            print("currentlyLoading")
                        case .unexpectedIncrementalRemoval:
                            print("unexpectedIncrementalRemoval")
                        }
                    } else {
                        print("reload succeeded")
                    }
                }
    

    这适用于iOS 10.x或更高版本,它是强制CXCallDirectoryProvider.forceRequest(带上下文:CXCallDirectoryExtensionContext)的唯一方法

相关问题