FFRouter is a powerful and easy-to-use URL routing library in iOS, supporting URL Rewrite, allowing APPs to dynamically modify related routing logic after they are published. Find URLs based on matching, high efficiency. Integration and use are very simple!
Github link:FFRouter
Function
- Have basic URL registration, Route, unregister, print log, etc.
- Supports the use of wildcards (*) to register URLs
- Support URL Rewrite
- When Rewrite is supported, you can get the original URL parameters or URLComponents and can URL Encode or Decode them.
- Supports getting Objects through URLs
- Passing unconventional objects when supporting Route URLs
- Unified callbacks when Route is supported for an unregistered URL
Install
CocoaPods target 'MyApp' do pod 'FFRouter' end
run pod install
Manual installation
Add the FFRouter folder to your own project
How to use
first
#import ""
1. Basic use
/** Register url @param routeURL URL to register @param handlerBlock URL is called back after being Route */ + (void)registerRouteURL:(NSString *)routeURL handler:(FFRouterHandler)handlerBlock; /** Register a URL. After the URL registered in this way is Route, it can return an Object. @param routeURL URL to register @param handlerBlock URL after being Routeed, an Object can be returned in the callback */ + (void)registerObjectRouteURL:(NSString *)routeURL handler:(FFObjectRouterHandler)handlerBlock; /** Determine whether the URL can be Route (whether it has been registered) @param URL The URL to be judged @return Can it be Route */ + (BOOL)canRouteURL:(NSString *)URL; /** Route A URL @param URL URL to Router */ + (void)routeURL:(NSString *)URL; /** Route a URL with additional parameters @param URL URL to Router @param parameters Extra parameters */ + (void)routeURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters; /** Route A URL to get the returned Object @param URL URL to Router @return Returned Object */ + (id)routeObjectURL:(NSString *)URL; /** Route A URL with additional parameters to get the returned Object @param URL URL to Router @param parameters Extra parameters @return Returned Object */ + (id)routeObjectURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters; /** Route Callback when an unregistered URL is called @param handler callback */ + (void)routeUnregisterURLHandler:(FFRouterUnregisterURLHandler)handler; /** Unregister a URL @param URL The URL to be unregistered */ + (void)unregisterRouteURL:(NSString *)URL; /** Unregister all URLs */ + (void)unregisterAllRoutes; /** Whether to display Log, for debugging @param enable YES or NO, default is NO */ + (void)setLogEnabled:(BOOL)enable;
【Remark】
(1) Registration URL:
[FFRouter registerRouteURL:@"protocol://page/routerDetails/:id" handler:^(NSDictionary *routerParameters) { //The callback when the URL of Route matches the registered URL this time}]; [FFRouter registerRouteURL:@"wildcard://*" handler:^(NSDictionary *routerParameters) { //The callback when the URL of Route matches the registered URL this time}]; [FFRouter registerRouteURL:@"protocol://page/routerObjectDetails" handler:^(NSDictionary *routerParameters) { //The callback when the URL of Route matches the registered URL this time}];
The parameters in the URL can be obtained through routerParameters, routerParameters[FFRouterParameterURLKey] is the complete URL.
(2) When it is necessary to use the following methods:
+ (id)routeObjectURL:(NSString *)URL;
When Route a URL and get the return value, you need to register the URL using the following method:
+ (void)registerObjectRouteURL:(NSString *)routeURL handler:(FFObjectRouterHandler)handlerBlock;
And return the object to be returned in handlerBlock, for example:
//Register and return the necessary value[FFRouter registerObjectRouteURL:@"protocol://page/routerObjectDetails" handler:^id(NSDictionary *routerParameters) { NSString *str = @“Return to the necessaryObject”; return str; }]; //Get the returned valueNSString *ret = [FFRouter routeObjectURL:@"protocol://page/routerObjectDetails"];
(3) If you need to pass unconventional objects as parameters, such as UIImage, etc., you can use the following method:
[FFRouter routeURL:@"protocol://page/routerDetails?nickname=imlifengfeng" withParameters:@{@"img":[UIImage imageNamed:@"router_test_img"]}];
2、URL Rewrite
/** Go to rewrite a URL according to the Rules set @param url will be rewrited URL The URL after @return rewrite */ + (NSString *)rewriteURL:(NSString *)url; /** Add a RewriteRule @param matchRule Regular matching rules @param targetRule Conversion Rules */ + (void)addRewriteMatchRule:(NSString *)matchRule targetRule:(NSString *)targetRule; /** Add multiple RewriteRule at the same time, the format must be: @[@{@"matchRule":@"YourMatchRule",@"targetRule":@"YourTargetRule"},...] @param rules RewriteRules */ + (void)addRewriteRules:(NSArray<NSDictionary *> *)rules; /** Remove a RewriteRule @param matchRule matchRule that will be removed */ + (void)removeRewriteMatchRule:(NSString *)matchRule; /** Remove all RewriteRule */ + (void)removeAllRewriteRules;
【Remark】
(1) You can use regularity to add a Rewrite rule, for example: To implement the implementation of the URL:/search/atomic bomb, intercept it and use the local registered URL:protocol://page/routerDetails?product=atomic bomb to open.
First add a Rewrite rule:
[FFRouterRewrite addRewriteMatchRule:@"(?:https://)?/search/(.*)" targetRule:@"protocol://page/routerDetails?product=$1"];
After that, when opening URL:/search/atomic bomb, you will Rewrite to URL:protocol://page/routerDetails?product=atomic bomb.
[FFRouter routeURL:@/search/Atomic bomb];
(2) Multiple rules can be added at the same time through the following methods:
+ (void)addRewriteRules:(NSArray<NSDictionary *> *)rules;
The rules format must be the following format:
@[@{@"matchRule":@"YourMatchRule1",@"targetRule":@"YourTargetRule1"}, @{@"matchRule":@"YourMatchRule2",@"targetRule":@"YourTargetRule2"}, @{@"matchRule":@"YourMatchRule3",@"targetRule":@"YourTargetRule3"},]
(3) Reserved words in Rewrite rules:
- Get the corresponding parts in the standard URL through $scheme, $host, $port, $path, $query, $fragment. Get the full URL through $url
- Get the parameters taken out using parentheses in the matchRule regular via $1, $2, $3...
- $: The value of the original variable, $$: The value after the original variable URL Encode, $#: The value after the original variable URL Decode
For example: /search/Atomic Bomb Rewrite Rules (?:https://)?/search/(.*)
$1=Atomic bomb $$1=%e5%8e%9f%e5%ad%90%e5%bc%b9
Similarly, /search/%e5%8e%9f%e5%ad%90%e5%bc%b9 for Rewrite rules (?:https://)?/search/(.*)
$1=%e5%8e%9f%e5%ad%90%e5%bc%b9 $#1=Atomic bomb
3、FFRouterNavigation
Considering that the jumps between UIViewControllers are often configured with routing, an additional tool FFRouterNavigation is added to more conveniently control jumps between UIViewControllers. The specific usage method is as follows:
/** Whether to automatically hide the bottom TabBar when push @param hide Whether it is automatically hidden, default is NO */ + (void)autoHidesBottomBarWhenPushed:(BOOL)hide; /** Get the current ViewController @return Current ViewController */ + (UIViewController *)currentViewController; /** Get the current NavigationViewController @return return Current NavigationViewController */ + (nullable UINavigationController *)currentNavigationViewController; /** Push ViewController @param viewController ViewController pushed @param animated Whether to use animations */ + (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; /** Push ViewController, which can set whether the current ViewController is still retained @param viewController ViewController pushed @param replace Whether the current ViewController is still retained @param animated Whether to use animations */ + (void)pushViewController:(UIViewController *)viewController replace:(BOOL)replace animated:(BOOL)animated; /** Push multiple ViewControllers @param viewControllers ViewController Array @param animated Whether to use animations */ + (void)pushViewControllerArray:(NSArray *)viewControllers animated:(BOOL)animated; /** present ViewController @param viewController ViewController presented @param animated Whether to use animations @param completion callback */ + (void)presentViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^ __nullable)(void))completion; /** Close the current ViewController, push and present methods are common @param animated Whether to use animations */ + (void)closeViewControllerAnimated:(BOOL)animated;
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.