Swiftpack.co - Package - steamclock/switchcraft

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 in to 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/netable.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.

Github

link
Stars: 3

Dependencies

Used By

Total: 0

Releases

Migrate to Swift 4.2 - 2019-01-10 17:54:52

  • No code changes needed in this migration
  • Updated configurations in Xcode, podspec, readme, and changelog to make all Swift version specifiers point to 4.2

Better Switcher Active Endpoint Icon - 2018-08-03 15:41:17

Improved the UI for showing the active endpoint in the switcher.

Add custom actions - 2018-07-09 18:37:11

Added support for custom actions within Switchcraft!

- 2018-07-05 17:22:00

Added a fallback for if CFBundleDisplayName is nil.

Add build information to switcher - 2018-06-11 16:42:39

Changed the default alert message from nil to contain the version and build number.