Giter VIP home page Giter VIP logo

easyspotlight's Introduction

Image

Build Status codecov codebeat badge Carthage compatible Version License Platform

Intro

EasySpotlight makes it easy to index contento to CoreSpotlight and handle app launches from Spotlight.

Requirements

CoreSpotlight is only available in iOS 9.0 or later

Installation

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"

Saving content to Spotlight

To use simply create a struct or class that implements the SpotlightConvertable protocol.

/**
*  Protocol that enables types to be saved to spotlight search
*/
public protocol SpotlightConvertable {

/// title displayed in search
var title: String? {get}

/// content description displayed in search
var contentDescription: String? {get}

/// unique idenfitier
var identifier: String {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.

There is also a helper SpotlightSyncRetrievable protocol in case your retrieval code is thread safe and you do not need the retrieving code to be executed on the same queue as the fetched item will be consumed on or not.

Sync / Thread Safe

Here is the simple example:

extension SimpleStruct: SpotlightSyncRetrievable {
    static func retrieveItem(with identifier: String) throws -> SimpleStruct? {
        return SimpleStruct(title: "item_\(identifier)", contentDescription: "cool", identifier: identifier)
    }
}

The method will be called in a background queue and then your registered handler will be called in the registered queue.

Async / Not thread safe

But if you are using something like Realm to save your objects (not thread safe), you need something like:

extension SimpleRealmClass: SpotlightRetrievable {
    static func retrieveItem(with identifier: String) throws -> (() throws -> SimpleRealmClass?) {
        let obj = try Realm().object(ofType: self, forPrimaryKey: identifier)
        let safe = obj.map { ThreadSafeReference(to: $0) }
        return {
            guard let safe = safe else { return nil }
            return try Realm().resolve(safe)
        }
    }
}

The method is still called in a background queue, but the returned closure will be executed in the same queue as the registered handler.

Register handler

Now you have to register a handler for that type in application:didFinishLaunchingWithOptions::

EasySpotlight.registerIndexableHandler(SimpleStruct.self, queue: .main) { item in
   // handle when item is opened from spotlight
   assert(item is SimpleStruct)
   print("started with: \(item)")
}

The queue parameter is optional and defaults to .main

Handle UserActivity

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)
}

Usage

See the included example application for a sample that indexes and retrieves objects from Realm.

Feedback

New feature ideas, suggestions, or any general feedback is greatly appreciated.

Author

Felix Dumit, [email protected]

License

EasySpotlight is available under the MIT license. See the LICENSE file for more info.

easyspotlight's People

Contributors

felix-dumit avatar jstart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

easyspotlight's Issues

Method 'retrieveItem(with:)' in non-final class 'Item' must return 'Self' to conform to protocol 'SpotlightRetrievable'

Hi Felix. I'm hoping to use this library with Realm in my app - but am getting this error:

Method 'retrieveItem(with:)' in non-final class 'Item' must return 'Self' to conform to protocol 'SpotlightRetrievable'

I have a Realm Object Class called 'Item'. The project is using Swift 4.2.

2018-11-30 at 11 47

Any help would be incredibly useful as I'm not sure why I'm getting this error - could it be a Swift 4.2 issue?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.