我已成功安装了一个扩展程序 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的信息。
reloadExtension()
方法。通过使用iOS的后台刷新、静默推送通知或用户手动更新等功能,应用程序可以了解到新数据。 - Stuart M