Skip to content

A drop-and-go endpoint selector and debug menu for iOS apps.

License

Notifications You must be signed in to change notification settings

steamclock/switchcraft

Repository files navigation

Switchcraft

Version License Platform

Switcher Switcher with custom URL

Contents

Description

Switchcraft is a simple tool designed to make switching between different endpoints a breeze.

It is designed to be dropped into an existing project and forgotten, but also supports configuring multiple instances and lots of other neat things.

Usage

Managing a Single Instance

The simplest way to use Switchcraft is to declare a single global instance, we recommend in your AppDelegate.swift, as follows:

extension Switchcraft {
    static let shared = Switchcraft(config: /*..*/)
}

Then, from your ViewController where you'd like to show the picker, all you need to do is attach the Switchcraft gesture recognizer to a view controller:

Switchcraft.shared.attachGesture(to: self)

Then you can retrieve the current endpoint from anywhere with

Switchcraft.shared.endpoint

To see this in action, check out the ReallySimpleExampleVC.

Keeping Current

To get updates whenever an endpoint is selected, you've got two options:

  1. Delegation

    If you only need to keep track of changes to the current endpoint in a single place, this is probably the way to go. Classes that want to receive updates only need to register your viewController as a delegate and conform to the SwitchcraftDelegate protocol.

    class MyVC: UIViewController {
        // ...
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            Switchcraft.delegate = self
        }
    }
    
    extension MyVC: SwitchcraftDelegate {
        func switchcraft(_ switchcraft: Switchcraft, didSelectEndpoint endpoint: Endpoint) {
            // Handle your endpoint selection here
        }
    }
    
  2. NotificationCenter

    Endpoint selections are also broadcast to the NotificationCenter.

    NotificationCenter.default.addObserver(self, selector: #selector(endpointSelected(_:)), name: .SwitchcraftDidSelectEndpoint, object: nil)
    
    ...
    
    @objc private func endpointSelected(_ sender: NSNotification) {
        guard let endpoint = sender.userInfo?[Notification.Key.Endpoint] as? Endpoint else {
            return
        }
        // Handle endpoint selected here
    }
    

Custom Actions

  1. Add some custom actions to Switchcraft via the Config:
extension Switchcraft {
    static let shared = Switchcraft(config: Config(
            defaultsKey: ...,
            endpoints: ...,
            actions: [
                Action(title: "Custom action 1", actionId: "customAction1"),
                Action(title: "Custom action 2", actionId: "customAction2")
            ]
        ))
}
  1. Add the following to your SwitchCraftDelegate:
extension MyVC: SwitchcraftDelegate {
    ...

    func switchcraft(_ switchcraft: Switchcraft, didTapAction action: Action)
        // Handle custom action selection here
    }
}

Note: We recommend using Swift enums for the actionId, like the following example:

enum Actions: String {
    case custom1
    case custom2
}

extension Switchcraft {
    static let shared = Switchcraft(config: Config(
            defaultsKey: ...,
            endpoints: ...,
            actions: [
                Action(title: "Custom action 1", actionId: Actions.custom1.rawValue),
                Action(title: "Custom action 2", actionId: Actions.custom2.rawValue)
            ]
        ))
}

extension MyVC: SwitchcraftDelegate {
    ...

    func switchcraft(_ switchcraft: Switchcraft, didTapAction action: Action) {
        guard let action = Actions(rawValue: action.actionId) else {
            return
        }

        switch action {
        case .custom1:
            // handle the first custom action tapped
            ...
        case .custom2:
            // handle the second custom action tapped
            ...
        }
    }
}

Getting Fancy

There are lots of knobs to tweak in your config. See Config.swift for a full list.

Example

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

Requirements

  • iOS 9.3 or above
  • Xcode 10 or above
  • Swift 4.2 or above

Installation

Swift Package Manager

Switchcraft is available through Swift Package Manager. To install it, follow these steps:

  1. In Xcode, click File, then Swift Package Manager, then Add Package Dependency
  2. Choose your project
  3. Enter this URL in the search bar https://github.com/steamclock/switchcraft.git

Cocoapods

Switchcraft is also available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Switchcraft'

Author

brendan@steamclock.com

License

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