EasySpotlight makes it easy to index contento to CoreSpotlight and handle app launches from Spotlight.
CoreSpotlight
is only available in iOS 9.0 or later
EasySpotlight is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "EasySpotlight"
Or through Carthage adding this to your Cartfile:
github "felix-dumit/EasySpotlight"
To use simply create a struct or class that implements the SpotlightConvertable
protocol.
@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)
}
Here is a simple example of a struct that implements the protocol and becomes indexable.
struct SimpleStruct:SpotlightConvertable {
var title:String? = nil
var identifier:String
}
You can now use all of the following methods:
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])
If you want to further configure the CSSearchableItem
and CSSearchbleAttributeItemSet
properties you can implement the configureSeachableItem
method in the protocol.
##Retrieving content
In order to easily handle when the app is opened via a spotlight search, you must implement the SpotlightRetrievable
protocol:
public protocol SpotlightRetrievable:SpotlightConvertable {
static func item(with identifier: String) -> Self?
}
Now you have to register a handler for that type in application:didFinishLaunchingWithOptions:
:
EasySpotlight.registerIndexableHandler(SimpleStruct.self) { item in
// handle when item is opened from spotlight
assert(item is SimpleStruct)
print("started with: \(item)")
}
Now all that is left is to implement the function called when the app opens from a spotlight search:
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
return EasySpotlight.handle(activity: userActivity) || handleOtherUserActivities(userActivity)
}
See the included example application for a sample that indexes and retrieves objects from Realm
.
New feature ideas, suggestions, or any general feedback is greatly appreciated.
Felix Dumit, felix.dumit@gmail.com
EasySpotlight is available under the MIT license. See the LICENSE file for more info.