60

GitHub - giginet/Crossroad: Route URL schemes easily

 6 years ago
source link: https://github.com/giginet/Crossroad
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

README.md

Crossroad

Build Status Language Carthage compatible CocoaPods Compatible Platform License

Route URL schemes easily.

Crossroad is URL router focused on handling Custom URL Scheme. Using this, you can route multiple URL schemes and fetch arguments and parameters easily.

Installation

CocoaPods

use_frameworks!

pod 'Crossroad'

Carthage

github "giginet/Crossroad"

Basic Usage

You can use DefaultRouter to define route definitions.

Imagine to implement Pokédex on iOS. You can access somewhere via URL scheme.

router = DefaultRouter(scheme: "pokedex")
router.register([
    ("pokedex://pokemons", { context in 
        let type: Type? = context.parameter(for: "type")
        presentPokedexListViewController(for: type)
        return true 
    }),
    ("pokedex://pokemons/:pokedexID", { context in 
        guard let pokedexID: Int = try? context.argument(for: "pokedexID") else {
            // pokedexID must be Int
            return false
        }
        if !Pokedex.isExist(pokedexID) { // Find the Pokémon by ID
            return false
        }
        presentPokedexDetailViewController(for: pokedex)
        return true 
    }),
    // ...
])

let canRespond25 = router.responds(to: URL(string: "pokedex://pokemons/25")!) // Pikachu(No. 25) is exist! so it returns true
let canRespond9999 = router.responds(to: URL(string: "pokedex://pokemons/9999")!) // No. 9999 is unknown. so it returns false
router.openIfPossible(URL(string: "pokedex://pokemons/25")) // Open Pikachu page
router.openIfPossible(URL(string: "pokedex://pokemons?type=fire")) // Open list of fire Pokémons page

In common use case, you should call router.openIfPossible on UIApplicationDelegate method.

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any]) -> Bool {
    return router.openIfPossible(url, options: options)
}

Argument and Parameter

: prefixed components on passed URL pattern mean argument.

For example, if passed URL matches pokedex://search/:keyword, you can get keyword from Context.

// matches: pokedex://search/Pikachu
let keyword: String = try! context.argument(for: "keyword") // Pikachu

And more, you can get query parameters if exist.

// matches: pokedex://search/Pikachu?generation=1
let generation: Int = context.parameter(for: "generation") // 1

Currently supported type is Int, Int64, Float, Double, Bool, String and URL.

Enum argument

You can use enum as arguments by implementing Argument.

enum Type: String, Argument {
    case normal
    case fire
    case water
    case grass
    // ....
}

// matches: pokedex://pokemons?type=fire
let type: Type = context.parameter(for: "type") // .fire

Comma-separated list

You can treat comma-separated query strings as Array.

// matches: pokedex://pokemons?types=water,grass
let types: [Type] = context.parameter(for: "types") // [.water, .grass]

Custom argument

You can also define own arguments by implementing Extractable. This is an example to parse custom struct.

struct User {
    let name: String
}
extension User: Extractable {
    static func extract(from string: String) -> User? {
        return User(name: string)
    }
}

Custom Router

You can add any payload to Router.

struct UserInfo {
    let userID: Int64
}
let router = Router<UserInfo>(scheme: "pokedex")
router.register([
    ("pokedex://pokemons", { context in 
        let userInfo: UserInfo = context.userInfo
        let userID = userInfo.userID
        return true
    }),
    // ...
])
let userInfo = UserInfo(userID: User.current.id)
router.openIfPossible(url, userInfo: userInfo)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK