麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統(tǒng) > iOS > 正文

淺談強(qiáng)大易用支持URL Rewrite的iOS路由庫FFRouter

2019-10-21 18:39:33
字體:
供稿:網(wǎng)友

FFRouter 是 iOS 中一個(gè)強(qiáng)大且易用的 URL 路由庫,支持 URL Rewrite,使 APP 在發(fā)布之后也可以動(dòng)態(tài)修改相關(guān)路由邏輯。基于匹配查找 URL,效率高。集成和使用都非常簡(jiǎn)單!

Github鏈接:FFRouter

功能

  • 具備基本的 URL 注冊(cè)、Route、取消注冊(cè)、打印 Log 等
  • 支持使用通配符(*)注冊(cè) URL
  • 支持 URL Rewrite
  • 支持 Rewrite 時(shí)獲取原 URL 參數(shù)或 URLComponents,并可對(duì)其進(jìn)行URL Encode或 Decode
  • 支持通過 URL 獲取 Object
  • 支持 Route URL 時(shí)傳遞非常規(guī)對(duì)象
  • 支持 Route 一個(gè)未注冊(cè)的 URL 時(shí)統(tǒng)一回調(diào)

安裝

CocoaPodstarget 'MyApp' do pod 'FFRouter'end

運(yùn)行 pod install

手動(dòng)安裝

添加其中的 FFRouter 文件夾到自己項(xiàng)目

使用方法

首先

#import "FFRouter.h"

1、基本使用

/** 注冊(cè) url @param routeURL 要注冊(cè)的 URL @param handlerBlock URL 被 Route 后的回調(diào) */+ (void)registerRouteURL:(NSString *)routeURL handler:(FFRouterHandler)handlerBlock;/** 注冊(cè) URL,通過該方式注冊(cè)的 URL 被 Route 后可返回一個(gè) Object @param routeURL 要注冊(cè)的 URL @param handlerBlock URL 被 Route 后的回調(diào),可在回調(diào)中返回一個(gè) Object */+ (void)registerObjectRouteURL:(NSString *)routeURL handler:(FFObjectRouterHandler)handlerBlock;/** 判斷 URL 是否可被 Route(是否已經(jīng)注冊(cè)) @param URL 要判斷的 URL @return 是否可被 Route */+ (BOOL)canRouteURL:(NSString *)URL;/** Route 一個(gè) URL @param URL 要 Router 的 URL */+ (void)routeURL:(NSString *)URL;/** Route 一個(gè) URL,并帶上額外參數(shù) @param URL 要 Router 的 URL @param parameters 額外參數(shù) */+ (void)routeURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters;/** Route 一個(gè) URL,可獲得返回的 Object @param URL 要 Router 的 URL @return 返回的 Object */+ (id)routeObjectURL:(NSString *)URL;/** Route 一個(gè) URL,并帶上額外參數(shù),可獲得返回的 Object @param URL 要 Router 的 URL @param parameters 額外參數(shù) @return 返回的 Object */+ (id)routeObjectURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters;/** Route 一個(gè)未注冊(cè) URL 時(shí)回調(diào) @param handler 回調(diào) */+ (void)routeUnregisterURLHandler:(FFRouterUnregisterURLHandler)handler;/** 取消注冊(cè)某個(gè) URL @param URL 要被取消注冊(cè)的 URL */+ (void)unregisterRouteURL:(NSString *)URL;/** 取消注冊(cè)所有 URL */+ (void)unregisterAllRoutes;/** 是否顯示 Log,用于調(diào)試 @param enable YES or NO,默認(rèn)為 NO */+ (void)setLogEnabled:(BOOL)enable;

【備注】

(1)注冊(cè) URL:

[FFRouter registerRouteURL:@"protocol://page/routerDetails/:id" handler:^(NSDictionary *routerParameters) { //Route的URL與本次注冊(cè)URL匹配時(shí)的回調(diào) }];[FFRouter registerRouteURL:@"wildcard://*" handler:^(NSDictionary *routerParameters) { //Route的URL與本次注冊(cè)URL匹配時(shí)的回調(diào) }];[FFRouter registerRouteURL:@"protocol://page/routerObjectDetails" handler:^(NSDictionary *routerParameters) { //Route的URL與本次注冊(cè)URL匹配時(shí)的回調(diào) }];

可通過routerParameters獲取 URL 中的參數(shù),routerParameters[FFRouterParameterURLKey]為完整的URL.

(2)當(dāng)需要通過以下方法:

+ (id)routeObjectURL:(NSString *)URL;

Route 一個(gè) URL 并獲取返回值時(shí),需要使用如下方法注冊(cè) URL:

+ (void)registerObjectRouteURL:(NSString *)routeURL handler:(FFObjectRouterHandler)handlerBlock;

并在 handlerBlock 中返回需要返回的 Object,例如:

//注冊(cè)并返回必要的值[FFRouter registerObjectRouteURL:@"protocol://page/routerObjectDetails" handler:^id(NSDictionary *routerParameters) {  NSString *str = @“根據(jù)需要返回必要的Object”;  return str; }]; //獲取返回的值NSString *ret = [FFRouter routeObjectURL:@"protocol://page/routerObjectDetails"];

(3)如果需要傳遞非常規(guī)對(duì)象作為參數(shù),如UIImage等,可使用如下方式:

[FFRouter routeURL:@"protocol://page/routerDetails?nickname=imlifengfeng" withParameters:@{@"img":[UIImage imageNamed:@"router_test_img"]}];

2、URL Rewrite

/** 根據(jù)設(shè)置的 Rules 去 rewrite 一個(gè) URL @param url 將被 rewrite 的 URL @return rewrite 后的 URL */+ (NSString *)rewriteURL:(NSString *)url;/** 添加一個(gè) RewriteRule @param matchRule 正則匹配規(guī)則 @param targetRule 轉(zhuǎn)換規(guī)則 */+ (void)addRewriteMatchRule:(NSString *)matchRule targetRule:(NSString *)targetRule;/** 同時(shí)添加多個(gè) RewriteRule,格式必須為:@[@{@"matchRule":@"YourMatchRule",@"targetRule":@"YourTargetRule"},...] @param rules RewriteRules */+ (void)addRewriteRules:(NSArray<NSDictionary *> *)rules;/** 移除一個(gè) RewriteRule @param matchRule 將被移除的 matchRule */+ (void)removeRewriteMatchRule:(NSString *)matchRule;/** 移除所有 RewriteRule */+ (void)removeAllRewriteRules;

【備注】

(1)可以使用正則添加一條 Rewrite 規(guī)則,例如:要實(shí)現(xiàn)打開 URL:https://www.taobao.com/search/原子彈時(shí),將其攔截,改用本地已注冊(cè)的URL:protocol://page/routerDetails?product=原子彈打開。

首先添加一條 Rewrite 規(guī)則:

[FFRouterRewrite addRewriteMatchRule:@"(?:https://)?www.taobao.com/search/(.*)" targetRule:@"protocol://page/routerDetails?product=$1"];

之后在打開URL:https://www.taobao.com/search/原子彈時(shí),將會(huì) Rewrite 到URL:protocol://page/routerDetails?product=原子彈。

[FFRouter routeURL:@https://www.taobao.com/search/原子彈];

(2)可以通過以下方法同時(shí)增加多個(gè)規(guī)則:

+ (void)addRewriteRules:(NSArray<NSDictionary *> *)rules;

其中 rules 格式必須為以下格式:

@[@{@"matchRule":@"YourMatchRule1",@"targetRule":@"YourTargetRule1"}, @{@"matchRule":@"YourMatchRule2",@"targetRule":@"YourTargetRule2"}, @{@"matchRule":@"YourMatchRule3",@"targetRule":@"YourTargetRule3"},]

(3)Rewrite 規(guī)則中的保留字:

  • 通過 $scheme、$host、$port、$path、$query、$fragment 獲取標(biāo)準(zhǔn) URL 中的相應(yīng)部分。通過$url獲取完整 URL
  • 通過 $1、$2、$3...獲取matchRule的正則中使用圓括號(hào)取出的參數(shù)
  • $:原變量的值、$$:原變量URL Encode后的值、$#:原變量URL Decode后的值

例如:https://www.taobao.com/search/原子彈對(duì)于Rewrite 規(guī)則(?: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對(duì)于Rewrite 規(guī)則(?:https://)?www.taobao.com/search/(.*)

$1=%e5%8e%9f%e5%ad%90%e5%bc%b9$#1=原子彈

3、FFRouterNavigation

考慮到經(jīng)常用路由配置UIViewController之間的跳轉(zhuǎn),所以增加了額外的工具FFRouterNavigation來更方便地控制UIViewController之間的跳轉(zhuǎn)。具體使用方法如下:

/** push 時(shí)是否自動(dòng)隱藏底部TabBar @param hide 是否自動(dòng)隱藏,默認(rèn)為 NO */+ (void)autoHidesBottomBarWhenPushed:(BOOL)hide; /** 獲取當(dāng)前 ViewController @return 當(dāng)前 ViewController */+ (UIViewController *)currentViewController;/** 獲取當(dāng)前 NavigationViewController @return return 當(dāng)前 NavigationViewController */+ (nullable UINavigationController *)currentNavigationViewController; /** Push ViewController @param viewController 被 Push 的 ViewController @param animated 是否使用動(dòng)畫 */+ (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;/** Push ViewController,可設(shè)置當(dāng)前 ViewController 是否還保留 @param viewController 被 Push 的 ViewController @param replace 當(dāng)前 ViewController 是否還保留 @param animated 是否使用動(dòng)畫 */+ (void)pushViewController:(UIViewController *)viewController replace:(BOOL)replace animated:(BOOL)animated;/** Push 多個(gè) ViewController @param viewControllers ViewController Array @param animated 是否使用動(dòng)畫 */+ (void)pushViewControllerArray:(NSArray *)viewControllers animated:(BOOL)animated;/** present ViewController @param viewController 被 present 的 ViewController @param animated 是否使用動(dòng)畫 @param completion 回調(diào) */+ (void)presentViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^ __nullable)(void))completion; /** 關(guān)閉當(dāng)前 ViewController,push、present 方式通用 @param animated 是否使用動(dòng)畫 */+ (void)closeViewControllerAnimated:(BOOL)animated;

 以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)VEVB武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 农村少妇吞精夜夜爽视频 | 亚洲国产精品久久久久久久 | 激情综合网俺也去 | 黄色片网站免费在线观看 | 黄色片网站免费在线观看 | 日韩精品二区 | 毛片在线免费播放 | 欧美不卡三区 | 美女擦逼 | 中国免费一级毛片 | 国产欧美在线观看不卡一 | 久久久久久久久亚洲精品 | 福利在线免费视频 | 日韩精品免费看 | 一级成人欧美一区在线观看 | 成人一区二区三区在线 | 中文字幕在线观看视频一区 | 毛片免费大全短视频 | 欧美国产成人在线 | 史上最强炼体老祖动漫在线观看 | 91九色视频观看 | 一及毛片视频 | 色淫网站免费视频 | 亚洲精中文字幕二区三区 | 日韩黄色免费观看 | 91看片王 | 久久99精品久久久久久国产越南 | 国产九九 | 在线成人一区二区 | 可以看毛片的网址 | 欧美18一12sex性处hd | 中文字幕四区 | av一二三四区 | 91情侣偷在线精品国产 | 国产亚洲精品久久久久婷婷瑜伽 | 亚洲一区二区不卡视频 | 亚洲成人精品一区二区 | 成人毛片在线 | 5xsq在线视频 | 成人性生活视频在线观看 | 深夜视频在线观看 |