ios – 使用Core Spotlight索引内容

ios – 使用Core Spotlight索引内容,第1张

概述在Mail应用程序或消息应用程序中,您可以使用Core Spotlight搜索搜索任何消息的内容.我也可以看到OneNote这样做,所以它应该在API中可用. 但是,关于这一点的文档几乎不存在.我只能看到在CSSearchableItemAttributeSet中有contentUrl,但我试图设置.txt文件的NSUrl并没有发生任何事情.还试图将contentType设置为kUTTypeTex 在Mail应用程序或消息应用程序中,您可以使用Core Spotlight搜索搜索任何消息的内容.我也可以看到OneNote这样做,所以它应该在API中可用.

但是,关于这一点的文档几乎不存在.我只能看到在CSSearchableItemAttributeSet中有contentUrl,但我试图设置.txt文件的NSUrl并没有发生任何事情.还试图将ContentType设置为kUTTypeText和kUTTypeUTF8PlainText但没有改进.

是否需要某些特定的文件格式?或者别的什么应该做的?

解决方法 Apple documentation on CoreSpotlight分解了在可搜索索引中创建和添加项目的过程:

Create a CSSearchableItemAttributeSet object and specify propertIEs that describe the item you want to index.

@H_404_26@

Create a CSSearchableItem object to represent the item. A CSSearchableItem object has a unique IDentifIEr that lets you refer to
it later.

@H_404_26@

If needed,specify a domain IDentifIEr so that you can gather multiple items together and manage them as a group.

@H_404_26@

Associate the attribute set with the searchable item.

@H_404_26@

Add the searchable item to the index.

@H_404_26@ @H_419_44@

这是一个快速示例,它展示了如何索引一个简单的Note类:

class Note {    var Title: String    var description: String    var image: UIImage?    init(Title: String,description: String) {        self.Title = Title        self.description = description    }}

然后在其他一些函数中,创建笔记,为每个笔记创建一个CSSearchableItemAttributeSet,从属性集创建一个唯一的CSSearchableItem,并索引可搜索项目的集合:

import CoreSpotlightimport MobileCoreServices// ...// Build your Notes data source to indexvar notes = [Note]()notes.append(Note(Title: "Grocery List",description: "Buy milk,eggs"))notes.append(Note(Title: "Reminder",description: "Soccer practice at 3"))let parkingReminder = Note(Title: "Reminder",description: "Soccer practice at 3")parkingReminder.image = UIImage(named: "parkingReminder")notes.append(parkingReminder)// The array of items that will be indexed by CoreSpotlightvar searchableItems = [CSSearchableItem]()for note in notes {    // create an attribute set of type Text,since our reminders are text    let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)    // If we have an image,add it to the attribute set    if let image = note.image {        searchableItemAttributeSet.thumbnailData = UIImagePNGRepresentation(image)        // you can also use thumbnailURL if your image is coming from a server or the bundle//        searchableItemAttributeSet.thumbnailURL = NSBundle.mainBundle().URLForResource("image",withExtension: "jpg")    }    // set the propertIEs on the item to index    searchableItemAttributeSet.Title = note.Title    searchableItemAttributeSet.contentDescription = note.description    // Build your keywords    // In this case,I'm tokenizing the Title of the note by a space and using the values returned as the keywords    searchableItemAttributeSet.keywords = note.Title.componentsSeparatedByString(" ")    // create the searchable item    let searchableItem = CSSearchableItem(uniqueIDentifIEr: "com.mygreatapp.notes" + ".\(note.Title)",domainIDentifIEr: "notes",attributeSet: searchableItemAttributeSet)}// Add our array of searchable items to the Spotlight indexCSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { (error) in    if let error = error {        // handle failure        print(error)    }}

这个例子改编自AppCoda’s How To Use Core Spotlight Framework in iOS 9指南.

总结

以上是内存溢出为你收集整理的ios – 使用Core Spotlight索引内容全部内容,希望文章能够帮你解决ios – 使用Core Spotlight索引内容所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1072932.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存