Giter VIP home page Giter VIP logo

ios-scotty's Introduction

Scotty

CI Status Version License Platform codecov codebeat badge

Purpose

This library provides a simple abstraction around the various entry points to an iOS application. URLs, application shortcut items, user activities, notification responses, and even custom types can be converted into a Route. These routes represent the various destinations your app can deep link too, allowing you to have a single code path through which all application links are executed.

Key Concepts

  • Route - An abstract representation of the code required to move your application from its root state to a specific final destination.
  • RouteController - An object that is created with a root view controller, and handles the execution of Routes.
  • RouteAction - An action that can be taken by the destination of a Route when travel has completed.

Usage

An instance of the RouteController should be created and retained somewhere accessible by your Application Delegate so that the relevant callbacks can be forwarded on to it. Although the RouteController can be wrapped and treated as a singleton, it does not have to be.

class AppDelegate: UIResponder, UIApplicationDelegate {
    var routeController: RouteController<UITabBarController>?
    //In this implementation, our root view controller is a UITabBarController

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
        if let window = window, let rootVC = window.rootViewController as? UITabBarController {
            routeController = RouteController(rootViewController: rootVC)
        }

        return true
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return routeController?.open(url.route) ?? false
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
        return routeController?.open(userActivity) ?? false
    }

    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        completionHandler(routeController?.open(shortcutItem) ?? false)
    }
}

In order to make URL/NSUserActivity/UIApplicationShortcutItem compatible with the RouteController, they will need to be extended to vend Route objects. A simple implementation might look like this:

extension URL {

    public var route: Route<UITabBarController>? {
        let components = URLComponents(url: self, resolvingAgainstBaseURL: false)!
        return Route.route(forIdentifier: components.path)
    }
}

When dealing with URLs, in addition to vending Route objects, you will also need to configure your app with its own URL scheme. More information on this process is available from Apple.

Creating Routes

Creating a new route is as simple as creating a new Route instance.

extension Route where RootViewController == UITabBarController {
    static var leftTab: Route {
		return Route(identifier: .leftTabRoute) { rootViewController, options -> Bool in
            rootViewController.selectedIndex = 0

            if let routeRespondableController = rootViewController.selectedViewController as? RouteRespondable {
                routeRespondableController.setRouteAction {
                    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                        print("LeftTab successfully reached!")
                    }
                }
            }

            return true
        }
    }
}

The above example creates a static instance of the Route struct designed to travel to the leftmost tab of the sample application. The identifier is used to differentiate this route from others when converting between types that vend routes (such as URL).

The trailing closure is the mechanism of routing. Given the instance of your root view controller, as well as any options provided with the routing request, this closure should execute the required changes to the view controller hierarchy to reach the destination. If the destination can successfully be reached, this closure should return true. Otherwise, it should return false.

Route Actions

Route Actions are closures that can be executed at particular route destinations. If the route conforms to the RouteRespondable protocol, it will have a public routeAction property which can be set inside the route. When this destination is reached, it will indicate to the routeAction an appropriate time at which to execute.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Requires iOS 9.0 +, Swift 4.1

Installation - CocoaPods

Add the following to your Podfile:

pod 'Scotty'

You will also need to make sure you're opting into using frameworks:

use_frameworks!

Then run pod install with CocoaPods 0.36 or newer.

Contributing

See the CONTRIBUTING document. Thank you, contributors!

ios-scotty's People

Contributors

wmcginty avatar br-tyler-milner avatar tylermilner avatar achappell avatar br-earl-gaspard avatar earlgaspard avatar

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.