任何App,無論大小,都存在繁雜的頁面跳轉(zhuǎn),按照正常的邏輯處理,那就是在發(fā)起的地方實(shí)例化一個(gè)targetVC,然后 push/present,但如果頻繁的這樣操作,你會(huì)發(fā)現(xiàn)隨著版本迭代,一旦出現(xiàn)代碼修改(原有VC命名修改、入?yún)⑿薷牡鹊龋?,你所要付出的代價(jià)也極大,而且風(fēng)險(xiǎn)極高,說不定漏掉了某個(gè)發(fā)起跳轉(zhuǎn)的地方?jīng)]有修改,導(dǎo)致產(chǎn)品線上報(bào)錯(cuò)甚至Crash掉;所以此種代碼耦合度是架構(gòu)設(shè)計(jì)時(shí)必須要考慮并解決的。所以受早期阿里相關(guān)開發(fā)人員的思維啟發(fā)(利用url進(jìn)行頁面跳轉(zhuǎn)),在2016年開始,我們小組內(nèi)部開始設(shè)計(jì)并使用相關(guān)的OC版路由跳轉(zhuǎn)工具,并在之后使用swift進(jìn)行了優(yōu)化,簡(jiǎn)單說一下實(shí)現(xiàn)思路,最后會(huì)附上源碼下載地址,方便大家使用。
封裝此工具的目標(biāo)
· 擺脫實(shí)例化target viewController 然后push/present的方式
· 節(jié)假日活動(dòng)彈窗、活動(dòng)頁面可以靈活調(diào)用跳轉(zhuǎn)其他本地頁面
關(guān)鍵思路整理
1、基于反射機(jī)制,我們可以通過字符串得到一個(gè)類甚至去創(chuàng)建一個(gè)對(duì)象。
2、建立一套繼承機(jī)制或者這協(xié)議,讓所有ViewController都執(zhí)行關(guān)鍵方法,這個(gè)方法中就可以進(jìn)行數(shù)據(jù)的傳遞,也就是通過此方法傳入?yún)?shù)
3、移動(dòng)端分為安卓、iOS,不可能要求頁面命名相同,那么也就意味著需要建立一個(gè)返回?cái)?shù)據(jù)的標(biāo)準(zhǔn),同時(shí),移動(dòng)端自己有一個(gè)翻譯工具,對(duì)返回?cái)?shù)據(jù)進(jìn)行規(guī)范化解析以正確翻譯跳轉(zhuǎn)指令
4、為了方便本地譯本日后的維護(hù)擴(kuò)展,并且容易閱讀了解,可以考慮創(chuàng)建一個(gè)plist文件進(jìn)行統(tǒng)一管理
5、一段url中可以拿到host、body、和參數(shù)等等,所以后臺(tái)可以將指令拼接成一段完整的url,移動(dòng)端解析并跳轉(zhuǎn)
方案設(shè)計(jì)

整體思路就是這樣,其實(shí)核心點(diǎn)就在于指令的處理,下面把我的工具發(fā)出來,大家可以自由下載使用。
工具需要在AppDelegate中進(jìn)行服務(wù)注冊(cè)和基本配置
/// 配置全局路由
func installRouter() {
TSRouter.shared.transferOriginViewController = {
let currentAppDele = UIApplication.shared.delegate as! AppDelegate
if currentAppDele.window?.rootViewController == nil {
debugPrint("TSRouter: window.rootViewController is not find")
return nil
}
let rootVC: UIViewController = self.root
var topVC: UIViewController?
if rootVC is UITabBarController {
topVC = (rootVC as! UITabBarController).selectedViewController
if topVC is TSNavigationController {
if let vc = ((topVC as! TSNavigationController).visibleNavitionController) {
topVC = vc
}
}
} else if rootVC is UINavigationController {
topVC = rootVC
}
while ((topVC?.presentedViewController) != nil) {
topVC = (topVC?.presentedViewController)!
if topVC is TSNavigationController {
if let vc = ((topVC as! TSNavigationController).visibleNavitionController) {
topVC = vc
}
}
}
return topVC
}
TSRouter.shared.presentDestinationViewController = { (viewController) in
return TSNavigationController(rootViewController: viewController)
}
TSRouter.shared.transferTabbarViewController = {
if self.root is UITabBarController {
return self.root as? UITabBarController
}
return nil
}
TSRouter.shared.transferSpecialViewControllers = { (currentVC, path, parserDic) in
if path.contains("main") {
currentVC.navigationController?.popToRootViewController(animated: false)
if self.root is UITabBarController {
(self.root as! UITabBarController).selectedIndex = 0
}
}
if path .contains("customerService") {
// 根據(jù)自定義服務(wù)標(biāo)識(shí), 進(jìn)行調(diào)度
}
}
TSRouter.shared.transferWebViewControllers = { (urlStr) in
let web = PBWebViewController()
web.htmlString = urlStr
TSRouter.routerPushVC(web)
}
TSRouter.shared.transferNeedRelySpecialStatus = { (vc) -> Bool in
if let token = PBUserInfo.shared.token, token.count > 0 {
return true
} else {
TSRouter.openUrl("hSchem://login")
return false
}
}
//這里可以隨意定義
TSRouter.shared.scheme = "hSchem"
//這里需要指明本地譯本所在,寫plist名稱即可
TSRouter.shared.moduleName = "moduleName"
}
另外,可被跳轉(zhuǎn)的頁面需要遵循協(xié)議并實(shí)現(xiàn)協(xié)議方法
extension ListDetailViewController : TSRouterProtocol {
func initWithRouter(routerData: [String : String]) {
if let itemId = routerData["itemId"] {
self.itemId = itemId
}
}
}
plist文件中我舉了幾個(gè)譯本的例子,可供參考,其中的transferStyle意義在于控制頁面跳轉(zhuǎn)的動(dòng)作,可查閱以下代碼來看
public enum TSRouterTransferStyle: String {
case push = "push"
case present = "present"
case tabTransfer = "transferTab" //tabbar 切換
case specialTransfer = "specialTransfer" //特殊跳轉(zhuǎn)
}
本地頁面跳轉(zhuǎn)需要明確如何跳轉(zhuǎn),所以在plist文件中可以設(shè)置4種動(dòng)作
設(shè)置完成后,具體使用代碼如下
//可以再需要跳轉(zhuǎn)頁面的地方調(diào)用如下代碼
//跳轉(zhuǎn)登陸頁面
TSRouter.openUrl("hSchem://login")
//跳轉(zhuǎn)詳情頁面并傳入 itemId 參數(shù) 1
TSRouter.openUrl("hSchem://listDetail?itemId=1")
//跳轉(zhuǎn)詳情頁面并傳入 itemId 參數(shù) 1,pageId 參數(shù) 2
TSRouter.openUrl("hSchem://listDetail?itemId=1&pageId=2")
// 這里的參數(shù)如果替換成后臺(tái)返回,就可以靈活配置跳轉(zhuǎn)頁面的動(dòng)作了
總結(jié)一下使用方法
1、AppDelegate中要對(duì)工具進(jìn)行配置
2、配置好本地譯本,也就是plist文件,其中className要對(duì)應(yīng)目標(biāo)頁面的類名,要配置跳轉(zhuǎn)方式,tabber主頁面需要指定transferStyle為 transferTab,需要特殊處理的可以標(biāo)記為specialTransfer,然后在AppDelegate中自行處理跳轉(zhuǎn)邏輯
3、參與跳轉(zhuǎn)的ViewController需要實(shí)現(xiàn)TSRouterProtocol協(xié)議的initWithRouter方法,方法內(nèi)可以接收入?yún)?br> 4、發(fā)起跳轉(zhuǎn)調(diào)用方法
TSRouter.openUrl("***")