SkillAgentSearch skills...

FFRouter

Powerful and easy-to-use URL routing library in iOS that supports URL Rewrite(强大、易用、支持 URL Rewrite的 iOS 路由库)

Install / Use

/learn @imlifengfeng/FFRouter
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<p align="center"> <img src="FFRouter_logo.png" title="FFRouter logo" float=left> </p> <p align="center"> <a href="http://cocoapods.org/pods/FFRouter"><img src="https://img.shields.io/cocoapods/v/FFRouter.svg?style=flat"></a> <a href="http://cocoapods.org/pods/FFRouter"><img src="https://img.shields.io/cocoapods/l/FFRouter.svg?style=flat"></a> <a href="http://cocoapods.org/pods/FFRouter"><img src="https://img.shields.io/cocoapods/p/FFRouter.svg?style=flat"></a> </p>

中文教程

FFRouter is a powerful and easy-to-use URL routing library in iOS that supports URL Rewrite, allowing the APP to dynamically modify the relevant routing logic after publishing. It is more efficient to find URLs based on matching rather than traversal. Integration and use are very simple!

function

  • [x] Have basic URL registration, Route, cancel registration, print Log, etc.
  • [x] Support forward and reverse pass values
  • [x] Support the use of wildcards (*) to register URL
  • [x] Support URL Rewrite
  • [x] Support get the original URL parameter or URLComponents when Rewrite, and can be URL Encode or Decode
  • [x] Support get an Object by URL
  • [x] Support get an Object by asynchronous callback when Route URL
  • [x] Support Transferring unconventional objects when Route URL
  • [x] Support Route an unregistered URL unified callback

install

CocoaPods

target 'MyApp' do
  pod 'FFRouter'
end

run pod install

Manual install

Add the FFRouter folder to your project

Usage

First

#import "FFRouter.h"
1、Basic Usage
/**
 Register URL,use it with 'routeURL:' and 'routeURL: withParameters:'.
 
 @param routeURL Registered URL
 @param handlerBlock Callback after route
 */
+ (void)registerRouteURL:(NSString *)routeURL handler:(FFRouterHandler)handlerBlock;

/**
 Register URL,use it with 'routeObjectURL:' and ‘routeObjectURL: withParameters:’,can return a Object.
 
 @param routeURL Registered URL
 @param handlerBlock Callback after route, and you can get a Object in this callback.
 */
+ (void)registerObjectRouteURL:(NSString *)routeURL handler:(FFObjectRouterHandler)handlerBlock;

/**
 Registered URL, use it with `routeCallbackURL: targetCallback:'and `routeCallback URL: withParameters: targetCallback:', calls back `targetCallback' asynchronously to return an Object
 
 @param routeURL Registered URL
 @param handlerBlock Callback after route,There is a `targetCallback' in `handlerBlock', which corresponds to the `targetCallback:' in `routeCallbackURL: targetCallback:'and `routeCallbackURL: withParameters: targetCallback:', which can be used for asynchronous callback to return an Object.
 */
+ (void)registerCallbackRouteURL:(NSString *)routeURL handler:(FFCallbackRouterHandler)handlerBlock;




/**
 Determine whether URL can be Route (whether it has been registered).
 
 @param URL URL to be verified
 @return Can it be routed
 */
+ (BOOL)canRouteURL:(NSString *)URL;




/**
 Route a URL
 
 @param URL URL to be routed
 */
+ (void)routeURL:(NSString *)URL;

/**
 Route a URL and bring additional parameters.
 
 @param URL URL to be routed
 @param parameters Additional parameters
 */
+ (void)routeURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters;

/**
 Route a URL and get the returned Object
 
 @param URL URL to be routed
 @return Returned Object
 */
+ (id)routeObjectURL:(NSString *)URL;

/**
 Route a URL and bring additional parameters. get the returned Object
 
 @param URL URL to be routed
 @param parameters Additional parameters
 @return Returned Object
 */
+ (id)routeObjectURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters;

/**
 Route a URL, 'targetCallBack' can asynchronously callback to return a Object.
 
 @param URL URL to be routed
 @param targetCallback asynchronous callback
 */
+ (void)routeCallbackURL:(NSString *)URL targetCallback:(FFRouterCallback)targetCallback;

/**
 Route a URL with additional parameters, and 'targetCallBack' can asynchronously callback to return a Object.
 
 @param URL URL to be routed
 @param parameters Additional parameters
 @param targetCallback asynchronous callback
 */
+ (void)routeCallbackURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters targetCallback:(FFRouterCallback)targetCallback;




/**
 Route callback for an unregistered URL
 
 @param handler Callback
 */
+ (void)routeUnregisterURLHandler:(FFRouterUnregisterURLHandler)handler;




/**
 Cancel registration of a URL
 
 @param URL URL to be cancelled
 */
+ (void)unregisterRouteURL:(NSString *)URL;

/**
 Unregister all URL
 */
+ (void)unregisterAllRoutes;



/**
 Whether to display Log for debugging
 
 @param enable YES or NO.The default is NO
 */
+ (void)setLogEnabled:(BOOL)enable;
【Notes】

(1)Register URL <br>Three types of URL:

[FFRouter registerRouteURL:@"protocol://page/routerDetails/:id" handler:^(NSDictionary *routerParameters) {
   //Callbacks of Route's URL match with this registration URL 
   //routerParameters contains all the parameters that are passed
}];

[FFRouter registerRouteURL:@"wildcard://*" handler:^(NSDictionary *routerParameters) {
   //Callbacks of Route's URL match with this registration URL
   //routerParameters contains all the parameters that are passed  
}];

[FFRouter registerRouteURL:@"protocol://page/routerObjectDetails" handler:^(NSDictionary *routerParameters) {
   //Callbacks of Route's URL match with this registration URL 
   //routerParameters contains all the parameters that are passed
}];

The parameters in the URL can be obtained by routerParametersrouterParameters[FFRouterParameterURLKey]Is the full URL. <br><br>Three ways of registration and Route:

//Way 1:
+ (void)registerRouteURL:(NSString *)routeURL handler:(FFRouterHandler)handlerBlock;

//Use the following two Route methods
+ (void)routeURL:(NSString *)URL;
+ (void)routeURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters;



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

//Use the following two Route methods
+ (id)routeObjectURL:(NSString *)URL;
+ (id)routeObjectURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters;



//Way 3:
+ (void)registerCallbackRouteURL:(NSString *)routeURL handler:(FFCallbackRouterHandler)handlerBlock;

//Use the following two Route methods
+ (void)routeCallbackURL:(NSString *)URL targetCallback:(FFRouterCallback)targetCallback;
+ (void)routeCallbackURL:(NSString *)URL withParameters:(NSDictionary<NSString *, id> *)parameters targetCallback:(FFRouterCallback)targetCallback;

<br><br><br>(2)When you need to use the following methods:

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

Route a URL and get the return value, you need to register URL with the following methods:

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

And return the corresponding Object in handlerBlock, for example:

//Register and return the necessary values
[FFRouter registerObjectRouteURL:@"protocol://page/routerObjectDetails" handler:^id(NSDictionary *routerParameters) {
        NSString *str = @“According to the need to return the necessary Object”;
        return str;
    }];
    
//Gets the returned value
NSString *ret = [FFRouter routeObjectURL:@"protocol://page/routerObjectDetails"];

<br><br>(3)If you want to get the returned Object through an asynchronous callback after routeURL, you can register and Route URL using the following method:

//Register URL and return to Object by callback at the necessary time.
[FFRouter registerCallbackRouteURL:@"protocol://page/RouterCallbackDetails" handler:^(NSDictionary *routerParameters, FFRouterCallback targetCallBack) {
       //When necessary, return to Object through'targetCallBack'callback.
       targetCallBack(@"Any Object");
}];

//When Route URL, by 'targetCallback' callbacks to get Object returned
[FFRouter routeCallbackURL:@"protocol://page/RouterCallbackDetails?nickname=imlifengfeng" targetCallback:^(id callbackObjc) {
        self.testLabel.text = [NSString stringWithFormat:@"%@",callbackObjc];
    }];

<br><br>(4)If you need to transfer non conventional objects as parameters, such as UIImage,You can use the following ways :

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

If you only need to pass common parameters, you can splice parameters directly after URL:

[FFRouter routeURL:@"protocol://page/routerDetails?nickname=imlifengfeng&id=666&parameters......"];

Then get these parameters from routerParameters. for example:routerParameters[@"nickname"]

2、URL Rewrite
/**
 According to the set of Rules, go to rewrite URL.

 @param url URL to be rewritten
 @return URL after being rewritten
 */
+ (NSString *)rewriteURL:(NSString *)url;

/**
 Add a RewriteRule

 @param matchRule Regular matching rule
 @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 to be removed
 */
+ (void)removeRewriteMatchRule:(NSString *)matchRule;

/**
 Remove all RewriteRule
 */
+ (void)removeAllRewriteRules;
【Notes】

(1)You can add a Rewrite rule using regular, for example: you want to When URL:https://www.amazon.com/search/Atomic_bomb is opened, intercept it and use the locally registered URL:protocol://page/routerDetails?product=Atomic_bomb to

View on GitHub
GitHub Stars304
CategoryCustomer
Updated6mo ago
Forks36

Languages

Objective-C

Security Score

92/100

Audited on Sep 20, 2025

No findings