前言
目前有很多市面上很不錯的URL
跳轉路由器,例如 MGJRouter、CTMediator、HHRouter 。
本著簡潔、易維護、符合業務、編碼方便的前提,簡單研究了下源碼之后,HHRouter 簡潔的代碼讓人有cover住的信心,巧妙的用法也大大提升了效率。
HHRouter 的缺點
- 需要每個類在
+load
方法注冊,感覺統計和維護起來并沒那么直觀 - 獲取到
controller
之后才能傳遞參數
我們希望在plist
表里就能看到URL
對應的關系,openURL
時可以傳遞相應的param
,借鑒 MGJRouter使用的思想,于是在MGJRouter和HHRouter的基礎上實現了ALRouter。
使用
- 在
plist
里面添加相應的鍵值對,URL
為key
,類名為value
,即使你已經進行組件化,依然可以在plist
文件注冊pod
中的ViewController
。 - 在
application:(UIApplication *)application didFinishLaunchingWithOptions:
注冊URL對應的類名:
[ALRouter loadConfigPlist:nil];
也可以直接注冊某個controller
[ALRouter regist:@"GoodsDetailController" toControllerClass:[self class]];
3.通過URL獲取controller
[self.navigationController pushViewController:[ALRouter openURL:@"GoodsDetail"] animated:YES];
或者傳遞參數
[self presentViewController:[ALRouter openURL:@"GoodsDetail" withParams:@{}] animated:YES completion:nil];
End
github地址在這里
我的組件化系列文章:
《iOS組件化》組件化實踐
《iOS組件化》組件的劃分
《iOS組件化》創建公共/私有 Pods
《iOS組件化》之使用AOP代替繼承
《iOS組件化》之搭建基于AFNetworking的網絡請求框架