用于解耦 Swift 路由和模块通信的工具。
基于 target-action 方案的组件化路由中间件。
支持使用字符串类名反射创建对象,并通过传递字典参数使用 Mirror 反射属性来赋值,通过字符串实现解耦,支持页面跳转和函数方法执行调用。
支持使用 OpenURL 方法跳转到页面并传递参数。
可以在模块之间不进行耦合的情况下调用服务和页面跳转。无需注册,无需协商,只需要知道目标VC的类名和模块名。
AppDelegate 和 SceneDelegate 解耦工具只需在主项目中留下钩子即可。请参阅 Demo 以获取使用细节。
![]() |
![]() |
---|---|
安装
Cocoapods
-
在 Podfile 中添加
pod 'SwiftMediator'
-
执行
pod install 或 pod update
-
导入
import SwiftMediator
Swift Package Manager
从 Xcode 11 开始,Swift Package Manager 集成,使用起来非常方便。SwiftMediator 也支持通过 Swift Package Manager 集成。
在Xcode菜单栏中选择 文件 > Swift包 > 添加包依赖
,并在搜索栏中输入
https://github.com/jackiehu/SwiftMediator
,即可完成集成
用法
本地显示或推送
SwiftMediator.shared.present(moduleName: “SwiftMediator”, toVC: “TestVC”,paramsDic: [“str”:”123123","titleName":"23452345”,”num”:13,”dic":["a":12,"b":"100"]])
SwiftMediator.shared.push(moduleName: “SwiftMediator”, toVC: “TestVC”,paramsDic: [“str”:”123123","titleName":"23452345","num”:13,”dic”:["a":12,"b":"100"]])
URL
SwiftMediator.shared.openUrl(“app://present/SwiftMediator/TestVC?str=123&titleName=456&num=111")
API
URL路由跳转
URL路由跳转 通过区分Push、present和fullScreen,并根据分割URL的scheme、host、path和query获取使用的参数
-
scheme: APP标记scheme,区分APP跳转,在APP中使用时可以传递任何值
-
host: push、present、fullScreen可以传递以区分跳转风格
-
path: /modulename/vcname,用于获取组件名称和VC名称
-
查询:通过 key=value&key=value 连接,可以转换为字典
/// URL routing jump Jump to distinguish Push, present, fullScreen /// - Parameter urlString: Call native page function scheme ://push/moduleName/vcName?quereyParams public func openUrl(_ urlString: String?)
推送
/// Routing Push
/// - Parameters:
/// - fromVC: Jump from that page--if not passed, the top VC is taken by default
/// - moduleName: The name of the component where the target VC is located
/// - vcName: target VC name
/// - paramsDic: parameter dictionary
/// - animated: whether there is animation
public func push(_ vcName: String,
moduleName: String? = nil,
fromVC: UIViewController? = nil,
paramsDic:[String:Any]? = nil,
animated: Bool = true)
/// Simple Push, initialize VC in advance
/// - Parameters:
/// - vc: initialized VC object
/// - fromVC: From which page to push, if not, the route selects the top VC
/// - animated: whether there is animation
public func push(_ vc: UIViewController?,
fromVC: UIViewController? = nil,
animated: Bool = true)
展示
/// route present
/// - Parameters:
/// - fromVC: Jump from that page--if not passed, the top VC is taken by default
/// - moduleName: The name of the component where the target VC is located
/// - vcName: target VC name
/// - paramsDic: parameter dictionary
/// - modelStyle: 0: modal style is default, 1: full screen modal, 2: custom
/// - needNav: Whether to need a navigation bar
/// - animated: whether there is animation
public func present(_ vcName: String,
moduleName: String? = nil,
paramsDic:[String:Any]? = nil,
fromVC: UIViewController? = nil,
needNav: Bool = true,
modelStyle: Int = 0,
animated: Bool = true)
/// Simple present, initialize VC in advance
/// - Parameters:
/// - vc: initialized VC object
/// - fromVC: From which page to push, if not, the route selects the top VC
/// - needNav: Whether to need a navigation bar
/// - modelStyle: 0: modal style is default, 1: full screen modal, 2: custom
/// - animated: whether there is animation
public func present(_ vc: UIViewController?,
fromVC: UIViewController? = nil,
needNav: Bool = true,
modelStyle: Int = 0,
animated: Bool = true)
获取最顶层 UIViewController
-
当前导航控制器
/// Get the top-level UINavigationController according to the window public func currentNavigationController() -> UINavigationController?
-
当前视图控制器
/// Get the top-level UIViewController according to the window public func currentViewController() -> UIViewController?
初始化 UIViewController
/// Reflect VC initialization and assignment
/// - Parameters:
/// - moduleName: component boundle name, if not passed, it will be the default namespace
/// - vcName: VC name
/// - dic: parameter dictionary // Since it is a KVC assignment, @objc must be marked on the parameter
@discardableResult
public func initVC(_ vcName: String,
moduleName: String? = nil,
dic: [String : Any]? = nil) -> UIViewController?
初始化对象
/// Reflect objc initialization and assignment Inherit NSObject
/// - Parameters:
/// - objcName: objcName
/// - moduleName: moduleName
/// - dic: parameter dictionary // Since it is a KVC assignment, @objc must be marked on the parameter
/// - Returns: objc
@discardableResult
public func initObjc(_ objcName: String,
moduleName: String? = nil,
dic: [String : Any]? = nil) -> NSObject?
方法调用:支持闭包块等参数,请参考示例了解详细用法
/// Routing call instance object method: @objc must be marked Example: @objc class func qqqqq(_ name: String)
/// - Parameters:
/// - objc: initialized object
/// - selName: method name
/// - param: parameter 1
/// - otherParam: parameter 2
@discardableResult
public func callObjcMethod(objc: AnyObject,
selName: String,
param: Any? = nil,
otherParam: Any? = nil ) -> Unmanaged<AnyObject>?
/// Routing call class method: @objc must be marked Example: @objc func qqqqq(_ name: String)
/// - Parameters:
/// - moduleName: component name
/// - className: class name
/// - selName: method name
/// - param: parameter 1
/// - otherParam: parameter 2
@discardableResult
public func callClassMethod(className: String,
selName: String,
moduleName: String? = nil,
param: Any? = nil,
otherParam: Any? = nil ) -> Unmanaged<AnyObject>?
AppDelegateMediator 解耦
用于 AppDelegate 解耦,可以为各种第三方初始化创建多个钩子
用法
///Create one or more hooks
class AppDe: AppDelegateMediator{
var window: UIWindow?
init(_ win : UIWindow?) {
window = win
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
print("UIApplication在这启动")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
print("UIApplication在这将要进入后台")
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
///Initialize the manager in AppDelegate and pass the array of hooks
lazy var manager: AppDelegateManager = {
return AppDelegateManager.init(delegates: [AppDe.init(window)])
}()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
/// Hand over the agent execution to the manager
manager.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
SceneDelegateMediator 解耦
用于 SceneDelegate 解耦,iOS13之后可以使用,可以为各种第三方初始化创建多个钩子
用法:
@available(iOS 13.0, *)
///Create one or more hooks
class SceneDe: SceneDelegateMediator{
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
print("UIScene在这启动")
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneWillResignActive(_ scene: UIScene) {
print("UIScene在这将要进入后台")
}
}
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
///Initialize the manager in SceneDelegate and pass the array of hooks
lazy var manager: SceneDelegateManager = {
return SceneDelegateManager.init(delegates: [SceneDe()])
}()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
/// Hand over the agent execution to the manager
manager.scene(scene, willConnectTo: session, options: connectionOptions)
guard let _ = (scene as? UIWindowScene) else { return }
}