欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談強大易用支持URL Rewrite的iOS路由庫FFRouter

 更新時間:2018年10月03日 10:36:51   作者:imlifengfeng  
FRouter 是 iOS 中一個強大且易用的 URL 路由庫,支持 URL Rewrite,基于匹配查找 URL,效率高。非常具有實用價值,需要的朋友可以參考下

FFRouter 是 iOS 中一個強大且易用的 URL 路由庫,支持 URL Rewrite,使 APP 在發(fā)布之后也可以動態(tài)修改相關(guān)路由邏輯?;谄ヅ洳檎?URL,效率高。集成和使用都非常簡單!

Github鏈接:FFRouter

功能

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

安裝

CocoaPods
target 'MyApp' do
 pod 'FFRouter'
end

運行 pod install

手動安裝

添加其中的 FFRouter 文件夾到自己項目

使用方法

首先

#import "FFRouter.h"

1、基本使用

/**
 注冊 url

 @param routeURL 要注冊的 URL
 @param handlerBlock URL 被 Route 后的回調(diào)
 */
+ (void)registerRouteURL:(NSString *)routeURL handler:(FFRouterHandler)handlerBlock;

/**
 注冊 URL,通過該方式注冊的 URL 被 Route 后可返回一個 Object

 @param routeURL 要注冊的 URL
 @param handlerBlock URL 被 Route 后的回調(diào),可在回調(diào)中返回一個 Object
 */
+ (void)registerObjectRouteURL:(NSString *)routeURL handler:(FFObjectRouterHandler)handlerBlock;



/**
 判斷 URL 是否可被 Route(是否已經(jīng)注冊)

 @param URL 要判斷的 URL
 @return 是否可被 Route
 */
+ (BOOL)canRouteURL:(NSString *)URL;



/**
 Route 一個 URL

 @param URL 要 Router 的 URL
 */
+ (void)routeURL:(NSString *)URL;

/**
 Route 一個 URL,并帶上額外參數(shù)

 @param URL 要 Router 的 URL
 @param parameters 額外參數(shù)
 */
+ (void)routeURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters;

/**
 Route 一個 URL,可獲得返回的 Object

 @param URL 要 Router 的 URL
 @return 返回的 Object
 */
+ (id)routeObjectURL:(NSString *)URL;

/**
 Route 一個 URL,并帶上額外參數(shù),可獲得返回的 Object

 @param URL 要 Router 的 URL
 @param parameters 額外參數(shù)
 @return 返回的 Object
 */
+ (id)routeObjectURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters;



/**
 Route 一個未注冊 URL 時回調(diào)

 @param handler 回調(diào)
 */
+ (void)routeUnregisterURLHandler:(FFRouterUnregisterURLHandler)handler;



/**
 取消注冊某個 URL

 @param URL 要被取消注冊的 URL
 */
+ (void)unregisterRouteURL:(NSString *)URL;

/**
 取消注冊所有 URL
 */
+ (void)unregisterAllRoutes;


/**
 是否顯示 Log,用于調(diào)試

 @param enable YES or NO,默認為 NO
 */
+ (void)setLogEnabled:(BOOL)enable;

【備注】

(1)注冊 URL:

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

[FFRouter registerRouteURL:@"wildcard://*" handler:^(NSDictionary *routerParameters) {
 //Route的URL與本次注冊URL匹配時的回調(diào) 
}];

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

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

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

+ (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 = @“根據(jù)需要返回必要的Object”;
  return str;
 }];
 
//獲取返回的值
NSString *ret = [FFRouter routeObjectURL:@"protocol://page/routerObjectDetails"];

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

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

2、URL Rewrite

/**
 根據(jù)設(shè)置的 Rules 去 rewrite 一個 URL

 @param url 將被 rewrite 的 URL
 @return rewrite 后的 URL
 */
+ (NSString *)rewriteURL:(NSString *)url;

/**
 添加一個 RewriteRule

 @param matchRule 正則匹配規(guī)則
 @param targetRule 轉(zhuǎn)換規(guī)則
 */
+ (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 規(guī)則,例如:要實現(xiàn)打開 URL:https://www.taobao.com/search/原子彈時,將其攔截,改用本地已注冊的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/原子彈時,將會 Rewrite 到URL:protocol://page/routerDetails?product=原子彈。

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

(2)可以通過以下方法同時增加多個規(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 獲取標準 URL 中的相應(yīng)部分。通過$url獲取完整 URL
  • 通過 $1、$2、$3...獲取matchRule的正則中使用圓括號取出的參數(shù)
  • $:原變量的值、$$:原變量URL Encode后的值、$#:原變量URL Decode后的值

例如:https://www.taobao.com/search/原子彈對于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對于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 時是否自動隱藏底部TabBar

 @param hide 是否自動隱藏,默認為 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 是否使用動畫
 */
+ (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

/**
 Push ViewController,可設(shè)置當(dāng)前 ViewController 是否還保留

 @param viewController 被 Push 的 ViewController
 @param replace 當(dāng)前 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 回調(diào)
 */
+ (void)presentViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^ __nullable)(void))completion;

 

/**
 關(guān)閉當(dāng)前 ViewController,push、present 方式通用

 @param animated 是否使用動畫
 */
+ (void)closeViewControllerAnimated:(BOOL)animated;

 以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • iOS App開發(fā)中Core Data框架基本的數(shù)據(jù)管理功能小結(jié)

    iOS App開發(fā)中Core Data框架基本的數(shù)據(jù)管理功能小結(jié)

    除了使用SQL關(guān)系型數(shù)據(jù)庫,我們還可以使用Xcode中提供的Core Data來進行表結(jié)構(gòu)數(shù)據(jù)處理,這里我們就來初步整理iOS App開發(fā)中Core Data框架基本的數(shù)據(jù)管理功能小結(jié):
    2016-06-06
  • Xcode 9下適配iPhoneX導(dǎo)致iOS 10不兼容問題的解決方法

    Xcode 9下適配iPhoneX導(dǎo)致iOS 10不兼容問題的解決方法

    這篇文章主要給大家介紹了關(guān)于Xcode 9下適配iPhoneX導(dǎo)致iOS 10不兼容問題的解決方法,文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • iOS實現(xiàn)波浪效果

    iOS實現(xiàn)波浪效果

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)波浪效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Xcode8 更新解決模擬器找不到的方法

    Xcode8 更新解決模擬器找不到的方法

    這篇文章主要介紹了Xcode8 更新解決模擬器找不到的方法的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • iOS狀態(tài)欄frame計算問題的實現(xiàn)

    iOS狀態(tài)欄frame計算問題的實現(xiàn)

    這篇文章主要介紹了iOS狀態(tài)欄frame計算問題的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • iOS實現(xiàn)高性能簡單易用的星星評分控件

    iOS實現(xiàn)高性能簡單易用的星星評分控件

    在做APP時會用到星星評分的一個視圖,在網(wǎng)上也找到一些相關(guān)的代碼,下面這篇文章主要給大家介紹了關(guān)于iOS實現(xiàn)高性能簡單易用的星星評分控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2018-03-03
  • 移動web開發(fā)技能之touch事件詳解

    移動web開發(fā)技能之touch事件詳解

    這篇文章主要為大家介紹了移動web開發(fā)技能之touch事件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • iOS中實現(xiàn)檢測Zoombie對象的具體方法

    iOS中實現(xiàn)檢測Zoombie對象的具體方法

    這篇文章主要給大家介紹了關(guān)于iOS中實現(xiàn)檢測Zoombie對象的具體方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • iOS中關(guān)于信鴿推送的使用demo詳解

    iOS中關(guān)于信鴿推送的使用demo詳解

    這篇文章主要介紹了iOS中關(guān)于信鴿推送的使用demo詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • ios 實現(xiàn)倒計時的兩種方式

    ios 實現(xiàn)倒計時的兩種方式

    這篇文章主要介紹了ios實現(xiàn)倒計時的兩種方式,第一種方式使用NSTimer來實現(xiàn),第二種方式使用GCD來實現(xiàn)。具體內(nèi)容詳情大家參考下本文
    2017-01-01

最新評論