首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让CoreSpotlight预测谁在打电话

如何让CoreSpotlight预测谁在打电话
EN

Stack Overflow用户
提问于 2017-12-13 09:38:42
回答 1查看 193关注 0票数 0

我想让我的设备根据聚光灯下的人指数来预测谁在打电话给我。我已经上传了人的信息到聚光灯指数和系统提供信息时,我正在搜索,但不是当有人打电话。下面的代码做了所有这些事情,我不明白是怎么回事

代码语言:javascript
复制
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")
            }
        })
    }

}

有人知道如何解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-13 11:59:16

过了很短时间,Paulw11说我需要用户CallKit扩展,所以有一个解决方案:

  1. 将新目标添加到项目"CallKIt扩展“中
  2. 创建应用程序组,为您的扩展名提供带有电话号码的文本文件,因为不可能使用那里的数据库
  3. 确保您的联系人按数字升序,以获得更好的性能。
  4. 将联系人写入文件 如果#available( .containerURL(forSecurityApplicationGroupIdentifier: 10.0,*) { let number= "79175870629“let label= "Stranger”//将其替换为id let groupId = "group.YOUR.ID“let容器= FileManager.default iOS groupId),则保护让fileUrl = FileManager.default fileUrl groupId)?.appendingPathComponent(”联系人“),否则在zip(数字,标签)中,{返回} var = "”(数字,标签){ string +=“(数字),(标签)\n”}尝试?CXCallDirectoryManager.sharedInstance.reloadExtension( string.write(to: fileUrl,原子化: true,编码:.utf8) string.write withIdentifier: groupId) { //回退早期版本}
  5. 然后将类LineReader添加到扩展from this post中。
  6. 调用reloadExtension时将调用此方法 覆盖func beginRequest(具有上下文: CXCallDirectoryExtensionContext) { context.delegate = self if #available(iOSApplicationExtension 11.0,*) { if context.isIncremental {CXCallDirectoryExtensionContext上下文)}{ addAllBlockingPhoneNumbers(to: context) addAllIdentificationPhoneNumbers(to: context) }beginRequest{ addAllBlockingPhoneNumbers(to: context) addAllIdentificationPhoneNumbers(to: context) } context.completeRequest() }
  7. 在我的例子中,我只实现了addAllIdentificationPhoneNumbers并从文件中读取那里的联系人。您需要将逻辑添加到默认生成的所有其他方法中。 fileUrl = FileManager.default FileManager.default "group.YOUR.ID")?.appendingPathComponent(“联系人”)另有{ reader }保护读取器=CBLineReader(路径: fileUrl.path)其他{返回} print("(#function) (fileUrl)"),用于读取器{自动释放池{ let line = line.trimmingCharacters( in:.whitespacesAndNewlines) var组件= line.components(separatedBy:",")保护let phone = Int64(components) let { name }let name= components1 components1 phone,label: name) print(#function + name) }}
  8. 转到设置,->,电话,->,呼叫阻塞和识别,->,打开你的应用程序对面的快速
  9. 测试你的应用程序:-)希望它能对某人有所帮助
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47789756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档