https://github.com/lixiang1994/apis
基于URLNavigator抽象的URL路由组件 灵感来自Moya 配置化 插件化.
https://github.com/lixiang1994/apis
ios moya navigation plugin router swift uri url
Last synced: 5 months ago
JSON representation
基于URLNavigator抽象的URL路由组件 灵感来自Moya 配置化 插件化.
- Host: GitHub
- URL: https://github.com/lixiang1994/apis
- Owner: lixiang1994
- License: mit
- Created: 2021-12-15T03:48:06.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-17T10:41:09.000Z (over 1 year ago)
- Last Synced: 2025-04-17T11:51:46.122Z (6 months ago)
- Topics: ios, moya, navigation, plugin, router, swift, uri, url
- Language: Swift
- Homepage:
- Size: 132 KB
- Stars: 18
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Apis
[](LICENSE)


[](https://github.com/Carthage/Carthage)
[](https://cocoapods.org)[URLNavigator](https://github.com/devxoul/URLNavigator) abstract routing component written in Swift, Inspired by [Moya](https://github.com/Moya/Moya).
## [天朝子民](README_CN.md)
## Features
- [x] Support for different processing based on plugin mechanism.
- [x] Configuration is independent and easy to manage.
- [x] Good business scalability.
- [x] Safer page management.
- [x] Support for asynchronous completion of callbacks.## Installation
Apis officially supports CocoaPods only.
**CocoaPods - Podfile**
```ruby
pod 'Apis'
```## Usage
First make sure to import the framework:
```swift
import Apis
```Here are some usage examples. All devices are also available as simulators:
### Create Apis
```swift
let router = Apis.Provider(
[RouterXXXXXXPlugin(),
RouterXXXXXXPlugin(),
RouterXXXXXXPlugin()]
)
```### TargetType
```swift
enum RouterTarget {
case open_http(url: URL)
case open_https(url: URL)
case open_none
case open_live(id: String)
case open_some
}extension RouterTarget: Apis.TargetType {
var task: Task {
switch(self) {
case let .open_http(url):
return .controller(SFSafariViewController(url: url))
case let .open_https(url):
return .controller(SFSafariViewController(url: url))
case .open_none:
return .controller(NoneViewController())
case let .open_live(id):
let controller = LiveViewController()
controller.id = id
return .controller(controller)
case .open_some:
return .handle { completion in
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
completion(true)
}
}
}
}
}extension XXXXViewController: Routerable { }
extension SFSafariViewController: Routerable { }
```
### URLTargetType```swift
private let schemes = "router"extension RouterTarget: URLTargetType {
static var bindings: [URLPatternBinding] {
return [
.init("http://") { source in
guard let url = source.url.value else { return .none }
return .open_http(url: url)
},
.init("https://") { source in
guard let url = source.url.value else { return .none }
return .open_https(url: url)
},
.init(schemes + "://open/none") {
return .open_none
},
.init(schemes + "://open/live") { source in
guard let id = source.url.queryParameters["id"] else { return nil }
return .open_live(id: id)
},
.init(schemes + "://open/some") {
return .open_some
}
]
}
}
```### Custom plugins
```swift
class RouterXXXXPlugin: Apis.PluginType {
func should(open target: TargetType) -> Bool {
/* ... */
return true
}
func prepare(open target: TargetType, completion: @escaping (Bool) -> Void) {
/* ... */
completion(true)
}
func will(open target: TargetType, controller: Routerable) {
/* ... */
}
func did(open target: TargetType, controller: Routerable) {
/* ... */
}
}
```### Open
```swift
// 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
}```
## Contributing
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 Apis yourself and want others to use it too, please submit a pull request.## License
Apis is under MIT license. See the [LICENSE](LICENSE) file for more info.