EasySpotlight 让您轻松将内容索引到 CoreSpotlight,并处理 Spotlight 启动应用。
CoreSpotlight
仅在 iOS 9.0 或更高版本的设备上可用
EasySpotlight 通过 CocoaPods 可用。要安装它,只需在 Podfile 中添加以下行
pod "EasySpotlight"
要使用它,只需创建一个实现 SpotlightConvertable
协议的结构体或类。
@available(iOS 9.0, *)
// implement to enable indexing methods
public protocol SpotlightConvertable:SpotlightIndexable {
var title:String? {get}
var identifier:StringLiteralType {get}
//optional
var itemType:String {get}
var thumbnailImage:UIImage? {get}
func configure(searchableItem item:CSSearchableItem)
}
这里是一个实现该协议并成为可搜索的结构体的简单示例。
struct SimpleStruct:SpotlightConvertable {
var title:String? = nil
var identifier:String
}
现在您可以使用以下所有方法
let x = SimpleStruct(title:"Bob", identifier:"identifier")
EasySpotlight.index(x) { error in
handleError(error)
}
EasySpotlight.deIndex(x)
EasySpotlight.deIndexAll(of: SimpleStruct.self)
EasySpotlight.index([x])
如果您想进一步配置 CSSearchableItem
和 CSSearchbleAttributeItemSet
属性,您可以在协议中实现 configureSeachableItem
方法。
为了轻松处理通过 Spotlight 搜索打开应用的情况,您必须实现 SpotlightRetrievable
协议
public protocol SpotlightRetrievable:SpotlightConvertable {
static func item(with identifier: String) -> Self?
}
现在您必须在 application:didFinishLaunchingWithOptions:
中为该类型注册一个处理程序。
EasySpotlight.registerIndexableHandler(SimpleStruct.self) { item in
// handle when item is opened from spotlight
assert(item is SimpleStruct)
print("started with: \(item)")
}
剩下要做的就是实现当应用从 Spotlight 搜索打开时的功能。
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
return EasySpotlight.handle(activity: userActivity) || handleOtherUserActivities(userActivity)
}
请查看附带的应用示例,以了解如何从 Realm
中索引和检索对象。
欢迎提供新功能想法、建议或任何一般性反馈。
Felix Dumit,[email protected]
EasySpotlight 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。