Skip to content

Commit 1a28042

Browse files
author
张鹏
committed
完善项目说明
1 parent 60b73bb commit 1a28042

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,83 @@ pod 'DPModuleManager', '~> 1.0.0'
3232

3333

3434

35+
#### 通过URL获取到对应的UIViewController
3536

37+
调用方通过*URL*获取对应的*UIViewController*对象,这里的URL为指定的字符串,你可以随意指定,但是个人建议和页面路由对应
38+
39+
这里以模拟的*问题详情页*来进行举例,这里的必要参数`questionID`,可以直接在URL中定义,也可以通过`info`参数传递,这取决于提供页面的模块如果返回对应的UIViewController对象
40+
41+
```swift
42+
guard let vc = ModuleManager.shared.viewController(withURL: "Question/Detail", info: ["questionID": "MyQuestionID"]) else {
43+
return
44+
}
45+
// ...
46+
```
47+
48+
> 除了可以通过ModuleManager来调用`viewController(withURL:, info:)`方法,也提供了Module中的便捷调用方法`Module.bus.viewController(withURL:, info:)`
49+
50+
在提供*问题详情页*的模块中,需要实现对应的**Module**的钩子方法
51+
52+
```swift
53+
// Question模块
54+
class QuestionModule: Module {
55+
56+
// 返回模块可以处理的URL
57+
override var canHandleURLs: [String]? {
58+
return [
59+
"Question/Detail"
60+
]
61+
}
62+
63+
// 返回URL对应的ViewController
64+
override func viewController(withURL url: String, info: [String : Any]?) -> UIViewController? {
65+
// ...
66+
if url == "Question/Detail", let questionID = info["questionID"] {
67+
let questionDetailVC = QuestionDetailViewController(questionID: questionID)
68+
return questionDetailVC
69+
}
70+
return nil
71+
}
72+
}
73+
```
74+
75+
76+
77+
#### 通过Service调用相关服务
78+
79+
这里通过调用模拟的*登录*服务来举例
80+
81+
```swift
82+
ModuleManager.shared.runService(withName: "User.SignIn",
83+
info: ["account": "MyAccount",
84+
"password": "MyPassword"]) { (callbackParams) in
85+
// ...
86+
}
87+
```
88+
89+
> 服务调用是异步进行
90+
91+
然后在*用户*模块中,需要对服务调用进行处理
92+
93+
```swift
94+
// 用户模块
95+
class MyModule: Module {
96+
97+
// 返回模块可以处理服务名称
98+
override var canHandleServiceNames: [String]? {
99+
return ["User.SignIn"]
100+
}
101+
102+
// 处理相关的服务调用
103+
override func runService(withName serviceName: String, info: [String : Any]?, callback: @escaping ([String : Any]) -> Void) -> Bool {
104+
if serviceName == "User.SignIn", let account = info["account"], let password = info["password"] {
105+
// Sign In Handling ...
106+
return true
107+
}
108+
return false
109+
}
110+
}
111+
```
36112

37113

38114

@@ -80,3 +156,9 @@ class MyModule: Module {
80156
}
81157
```
82158

159+
160+
161+
## LICENSE
162+
163+
此项目采用**MIT**开源协议,[点击查看详情](LICENSE)
164+

SwiftLib/Sources/SwiftLibViewController.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class SwiftLibViewController: UIViewController {
1313
override func viewDidLoad() {
1414
super.viewDidLoad()
1515

16+
SwiftLibModule.bus.runService(withName: "User/SignIn",
17+
info: ["account": "MyAccount",
18+
"password": "MyPassword"]) { (info) in
19+
20+
}
1621
}
1722

1823
@IBAction func pushObjCViewControllerButtonDidClick(_ sender: Any) {

0 commit comments

Comments
 (0)