我希望我的设备根据聚光灯人数指数预测谁在给我打电话。我已将人员信息上传到聚光灯索引,系统会在我搜索时提供信息,但在有人打电话时则不会。下面的代码做了所有这些事情,我无法理解什么是错的
if people.count > 0 {
var peopleArray = [CSSearchableItem]()
var peopleGUIDs = [String]()
for person in people {
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
// Basic AttributeSet setup
attributeSet.title = person.nameForList
attributeSet.contentDescription = person.division?.title
// Add first phone number to AttributeSet
var phoneNumber: NSString?
let contacts = Array(person.contacts)
for contact in contacts {
if contact.type == "phone" {
phoneNumber = contact.value as NSString
break
}
}
if phoneNumber != nil {
if let preparedNumber = phoneNumber!.removingPercentEncoding {
attributeSet.phoneNumbers = [preparedNumber]
attributeSet.supportsPhoneCall = true
}
}
attributeSet.displayName = person.name
// Add photo number to AttributeSet
if let photoPath = person.photo {
let key = SDWebImageManager.shared().cacheKey(for: NSURL(string: photoPath) as URL!)
let image = SDImageCache.shared().imageFromDiskCache(forKey: key)
var data = Data()
if let image = image {
if let dataFromImage = UIImagePNGRepresentation(image) {
data = dataFromImage
}
} else {
data = dataFromImage
}
attributeSet.thumbnailData = data
}
peoplesGUIDs.append(person.id)
let item = CSSearchableItem(uniqueIdentifier: person.id, domainIdentifier: "com.it.companySpotlight", attributeSet: attributeSet)
peopleArray.append(item)
}
CSSearchableIndex.default().indexSearchableItems(peopleArray) { (error) in
DispatchQueue.main.async(execute: {
if let error = error {
print("Indexing error: \(error.localizedDescription)")
} else {
print("Search for people successfully indexed")
}
})
}
}
有人知道如何解决这个问题吗?
答案 0 :(得分:0)
经过一段时间后,Paulw11说我需要使用CallKit扩展,所以有解决方案:
将联系人写入文件
if #available(iOS 10.0, *) {
let numbers = ["79175870629"]
let labels = ["Stranger name"]
// Replace it with your id
let groupId = "group.YOUR.ID"
let container = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: groupId)
guard let fileUrl = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: groupId)?
.appendingPathComponent("contacts") else { return }
var string = ""
for (number, label) in zip(numbers, labels) {
string += "\(number),\(label)\n"
}
try? string.write(to: fileUrl, atomically: true, encoding: .utf8)
CXCallDirectoryManager.sharedInstance.reloadExtension(
withIdentifier: groupId)
} else {
// Fallback on earlier versions
}
调用reloadExtension
时将调用此方法override func beginRequest(with context: CXCallDirectoryExtensionContext) {
context.delegate = self
if #available(iOSApplicationExtension 11.0, *) {
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并从文件中读取了联系人。您需要将逻辑添加到默认生成的所有其他方法
guard let fileUrl = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.YOUR.ID")?
.appendingPathComponent("contacts") else { return }
guard let reader = CBLineReader(path: fileUrl.path) else { return }
print("\(#function) \(fileUrl)")
for line in reader {
autoreleasepool {
let line = line.trimmingCharacters(in: .whitespacesAndNewlines)
var components = line.components(separatedBy: ",")
guard let phone = Int64(components[0]) else { return }
let name = components[1]
context.addIdentificationEntry(withNextSequentialPhoneNumber: phone, label: name)
print(#function + name)
}
}
转到设置 - >电话 - >呼叫阻止&识别 - >在您的应用程序对面快速打开
测试你的应用:-)希望它会对某人有所帮助