強大易用支援 URL Rewrite 的 iOS 路由庫 FFRouter
FFRouter 是 iOS 中一個強大且易用的 URL 路由庫,支援 URL Rewrite,使 APP 在釋出之後也可以動態修改相關路由邏輯。基於匹配查詢 URL,效率高。整合和使用都非常簡單!
Github連結:ofollow,noindex">FFRouter
功能
- 具備基本的 URL 註冊、Route、取消註冊、列印 Log 等
- 支援使用萬用字元(*)註冊 URL
- 支援 URL Rewrite
- 支援 Rewrite 時獲取原 URL 引數或 URLComponents,並可對其進行URL Encode或 Decode
- 支援通過 URL 獲取 Object
- 支援 Route URL 時傳遞非常規物件
- 支援 Route 一個未註冊的 URL 時統一回調
安裝
CocoaPods
target 'MyApp' do pod 'FFRouter' end
執行pod install
手動安裝
新增其中的FFRouter
資料夾到自己專案
使用方法
首先
#import "FFRouter.h"
1、基本使用
/** 註冊 url @param routeURL 要註冊的 URL @param handlerBlock URL 被 Route 後的回撥 */ + (void)registerRouteURL:(NSString *)routeURL handler:(FFRouterHandler)handlerBlock; /** 註冊 URL,通過該方式註冊的 URL 被 Route 後可返回一個 Object @param routeURL 要註冊的 URL @param handlerBlock URL 被 Route 後的回撥,可在回撥中返回一個 Object */ + (void)registerObjectRouteURL:(NSString *)routeURL handler:(FFObjectRouterHandler)handlerBlock; /** 判斷 URL 是否可被 Route(是否已經註冊) @param URL 要判斷的 URL @return 是否可被 Route */ + (BOOL)canRouteURL:(NSString *)URL; /** Route 一個 URL @param URL 要 Router 的 URL */ + (void)routeURL:(NSString *)URL; /** Route 一個 URL,並帶上額外引數 @param URL 要 Router 的 URL @param parameters 額外引數 */ + (void)routeURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters; /** Route 一個 URL,可獲得返回的 Object @param URL 要 Router 的 URL @return 返回的 Object */ + (id)routeObjectURL:(NSString *)URL; /** Route 一個 URL,並帶上額外引數,可獲得返回的 Object @param URL 要 Router 的 URL @param parameters 額外引數 @return 返回的 Object */ + (id)routeObjectURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters; /** Route 一個未註冊 URL 時回撥 @param handler 回撥 */ + (void)routeUnregisterURLHandler:(FFRouterUnregisterURLHandler)handler; /** 取消註冊某個 URL @param URL 要被取消註冊的 URL */ + (void)unregisterRouteURL:(NSString *)URL; /** 取消註冊所有 URL */ + (void)unregisterAllRoutes; /** 是否顯示 Log,用於除錯 @param enable YES or NO,預設為 NO */ + (void)setLogEnabled:(BOOL)enable;
【備註】
(1)註冊 URL:
[FFRouter registerRouteURL:@"protocol://page/routerDetails/:id" handler:^(NSDictionary *routerParameters) { //Route的URL與本次註冊URL匹配時的回撥 }]; [FFRouter registerRouteURL:@"wildcard://*" handler:^(NSDictionary *routerParameters) { //Route的URL與本次註冊URL匹配時的回撥 }]; [FFRouter registerRouteURL:@"protocol://page/routerObjectDetails" handler:^(NSDictionary *routerParameters) { //Route的URL與本次註冊URL匹配時的回撥 }];
可通過routerParameters
獲取 URL 中的引數,routerParameters[FFRouterParameterURLKey]
為完整的URL.
(2)當需要通過以下方法:
+ (id)routeObjectURL:(NSString *)URL;
Route 一個 URL 並獲取返回值時,需要使用如下方法註冊 URL:
+ (void)registerObjectRouteURL:(NSString *)routeURL handler:(FFObjectRouterHandler)handlerBlock;
並在 handlerBlock 中返回需要返回的 Object,例如:
//註冊並返回必要的值 [FFRouter registerObjectRouteURL:@"protocol://page/routerObjectDetails" handler:^id(NSDictionary *routerParameters) { NSString *str = @“根據需要返回必要的Object”; return str; }]; //獲取返回的值 NSString *ret = [FFRouter routeObjectURL:@"protocol://page/routerObjectDetails"];
(3)如果需要傳遞非常規物件作為引數,如UIImage
等,可使用如下方式:
[FFRouter routeURL:@"protocol://page/routerDetails?nickname=imlifengfeng" withParameters:@{@"img":[UIImage imageNamed:@"router_test_img"]}];
2、URL Rewrite
/** 根據設定的 Rules 去 rewrite 一個 URL @param url 將被 rewrite 的 URL @return rewrite 後的 URL */ + (NSString *)rewriteURL:(NSString *)url; /** 新增一個 RewriteRule @param matchRule 正則匹配規則 @param targetRule 轉換規則 */ + (void)addRewriteMatchRule:(NSString *)matchRule targetRule:(NSString *)targetRule; /** 同時新增多個 RewriteRule,格式必須為:@[@{@"matchRule":@"YourMatchRule",@"targetRule":@"YourTargetRule"},...] @param rules RewriteRules */ + (void)addRewriteRules:(NSArray<NSDictionary *> *)rules; /** 移除一個 RewriteRule @param matchRule 將被移除的 matchRule */ + (void)removeRewriteMatchRule:(NSString *)matchRule; /** 移除所有 RewriteRule */ + (void)removeAllRewriteRules;
【備註】
(1)可以使用正則
新增一條 Rewrite 規則,例如:
要實現開啟 URL:https://www.taobao.com/search/原子彈
時,將其攔截,改用本地已註冊的 URL:protocol://page/routerDetails?product=原子彈
開啟。
首先新增一條 Rewrite 規則:
[FFRouterRewrite addRewriteMatchRule:@"(?:https://)?www.taobao.com/search/(.*)" targetRule:@"protocol://page/routerDetails?product=$1"];
之後在開啟URL:https://www.taobao.com/search/原子彈
時,將會 Rewrite 到URL:protocol://page/routerDetails?product=原子彈
。
[FFRouter routeURL:@"https://www.taobao.com/search/原子彈"];
(2)可以通過以下方法同時增加多個規則:
+ (void)addRewriteRules:(NSArray<NSDictionary *> *)rules;
其中 rules 格式必須為以下格式:
@[@{@"matchRule":@"YourMatchRule1",@"targetRule":@"YourTargetRule1"}, @{@"matchRule":@"YourMatchRule2",@"targetRule":@"YourTargetRule2"}, @{@"matchRule":@"YourMatchRule3",@"targetRule":@"YourTargetRule3"},]
(3)Rewrite 規則中的保留字:
-
通過
$scheme
、$host
、$port
、$path
、$query
、$fragment
獲取標準 URL 中的相應部分。通過$url
獲取完整 URL -
通過
$1
、$2
、$3
...獲取matchRule
的正則中使用圓括號取出的引數 -
$
:原變數的值、$$
:原變數URL Encode後的值、$#
:原變數URL Decode後的值
例如:
https://www.taobao.com/search/原子彈
對於Rewrite 規則(?:https://)?www.taobao.com/search/(.*)
$1=原子彈 $$1=%e5%8e%9f%e5%ad%90%e5%bc%b9
同樣,https://www.taobao.com/search/%e5%8e%9f%e5%ad%90%e5%bc%b9
對於Rewrite 規則(?:https://)?www.taobao.com/search/(.*)
$1=%e5%8e%9f%e5%ad%90%e5%bc%b9 $#1=原子彈
2、FFRouterNavigation
考慮到經常用路由配置UIViewController
之間的跳轉,所以增加了額外的工具FFRouterNavigation
來更方便地控制UIViewController
之間的跳轉。具體使用方法如下:
/** push 時是否自動隱藏底部TabBar @param hide 是否自動隱藏,預設為 NO */ + (void)autoHidesBottomBarWhenPushed:(BOOL)hide; /** 獲取當前 ViewController @return 當前 ViewController */ + (UIViewController *)currentViewController; /** 獲取當前 NavigationViewController @return return 當前 NavigationViewController */ + (nullable UINavigationController *)currentNavigationViewController; /** Push ViewController @param viewController 被 Push 的 ViewController @param animated 是否使用動畫 */ + (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; /** Push ViewController,可設定當前 ViewController 是否還保留 @param viewController 被 Push 的 ViewController @param replace 當前 ViewController 是否還保留 @param animated 是否使用動畫 */ + (void)pushViewController:(UIViewController *)viewController replace:(BOOL)replace animated:(BOOL)animated; /** Push 多個 ViewController @param viewControllers ViewController Array @param animated 是否使用動畫 */ + (void)pushViewControllerArray:(NSArray *)viewControllers animated:(BOOL)animated; /** present ViewController @param viewController 被 present 的 ViewController @param animated 是否使用動畫 @param completion 回撥 */ + (void)presentViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^ __nullable)(void))completion; /** 關閉當前 ViewController,push、present 方式通用 @param animated 是否使用動畫 */ + (void)closeViewControllerAnimated:(BOOL)animated;
感謝
FFRouter
實現方案參考了以下文章,在此表示感謝!