URLNavigator abstract routing component written in Swift
Router officially supports CocoaPods only.
CocoaPods - Podfile
source 'https://github.com/lixiang1994/Specs'
pod 'Router'
First make sure to import the framework:
import Router
Here are some usage examples. All devices are also available as simulators:
let router = Provider(
[RouterXXXXXXPlugin(),
RouterXXXXXXPlugin(),
RouterXXXXXXPlugin()]
)
enum RouterType: RouterTypeable {
case open_http
case open_https
case open_xxxx
/* ... */
}
extension RouterType {
var pattern: String {
switch self {
case .open_http: return "http://<path:_>"
case .open_https: return "https://<path:_>"
case .open_xxxx: return "xxxx://open/xxxx"
/* ... */
}
}
func controller(url: URLConvertible, values: [String: Any]) -> Routerable? {
switch self {
case .open_http, .open_https:
guard let url = url.urlValue else { return nil }
return SFSafariViewController(url: url)
case .open_xxxx:
return XXXXViewController()
/* ... */
}
}
func handle(url: URLConvertible, values: [String : Any], completion: @escaping (Bool) -> Void) {
/* ... */
completion(true)
}
}
extension XXXXViewController: Routerable { }
extension SFSafariViewController: Routerable { }
class RouterXXXXPlugin: Plugin<RouterType> {
override func should(open type: RouterType) -> Bool {
/* ... */
return true
}
override func prepare(open type: RouterType, completion: @escaping (Bool) -> Void) {
/* ... */
completion(true)
}
override func will(open type: RouterType, controller: Routerable) {
/* ... */
}
override func did(open type: RouterType, controller: Routerable) {
/* ... */
}
}
// Open page based on type
router.open(.open_xxxx)
// Open page based on url
router.open("http://xxxxxxxx")
// Result callback
router.open("http://xxxxxxxx") { (result) in
// Success or failure
}
If you have the need for a specific feature that you want implemented or if you experienced a bug, please open an issue. If you extended the functionality of Router yourself and want others to use it too, please submit a pull request.
Router is under MIT license. See the LICENSE file for more info.