HZURLManager
适用于 iOS 的 URL 路由库,支持 URL 重写。
(它是 HZExtend 的一个组件)
联系方式
QQ 群:32272635
[email protected]
电子邮箱:预览
安装
CocoaPods
- 将以下指令添加到您的Podfile中:
pod 'HZURLManager'
。 - 运行
pod install
或pod update
。 - 导入 <HZURLManager/HZURLManager.h>。
文档
完整的API文档可在CocoaDocs上找到。
要求
此库需要iOS 8.0+和Xcode 8.0+。
许可证
HZURLManager根据MIT许可证提供。有关详细信息,请参阅LICENSE文件。
使用方式
URL 配置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Loads URL-Controller & URL-Method Config.
[[HZURLManagerConfig sharedConfig] loadURLCtrlConfig:[[NSBundle mainBundle] pathForResource:@"URL-Controller-Config" ofType:@"plist"] urlMethodConfig:[[NSBundle mainBundle] pathForResource:@"URL-Method-Config" ofType:@"plist"]];
//Adds URL Rewrite rule. You may be get the rule from remote.
/**
The variable can be used in the target and starts with $, For example, $1 ... $n represents the value of the corresponding tuple in the regular expression, $query represents the query string part in the URL.
For example, when the @{@"match":@"(?:https://)?www.hz.com/articles/(\\d)\\?(.*)",@"target":@"hz://page.hz/article?$query&id=$1"} rule is applied, the rewrite engine rewrites the source URL as hz://page.hz/article?title=cool&id=3 when we redirect to https://ww.hz.com/articles/3?title=cool , Finally we'll jump to hz://page.hz/article?title=cool&id=3.
*/
[[HZURLManagerConfig sharedConfig] addRewriteRules:@[@{@"match":@"(?:https://)?www.hz.com/articles/(\\d)\\?(.*)",@"target":@"hz://page.hz/article?$query&id=$1"}]];
//Configs the default name of controller for Http(s) URL.
[HZURLManagerConfig sharedConfig].classOfWebViewCtrl = @"WebViewController";
}
重定向
//Present
[URL_MANAGERN redirectToURL:@"hz://page.hz/article?title=present" animated:YES parmas:nil options:@{HZRedirectPresentMode:@(YES)} completion:nil];
//Push
//The following URL will be converted to hz://page.hz/article by rewriting.
[URL_MANAGERN redirectToURL:@"https://www.hz.com/articles/3?title=push" animated:YES];
执行方法
@interface ShowAlertURLHandler ()<HZURLHandler>
@end
@implementation ShowAlertURLHandler
/**
hz://urlmanger.kit/doAlert
@param title
@param message
*/
- (id)handleURL:(NSURL *)url withParams:(id)params
{
NSDictionary *queryParam = url.queryDic;
NSString *title = [queryParam objectForKey:@"title"];
NSString *message = [queryParam objectForKey:@"message"];
UIAlertController *alerController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirmAtion = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:nil];
[alerController addAction:confirmAtion];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"Cancle" style:UIAlertActionStyleCancel handler:nil];
[alerController addAction:cancleAction];
[[HZURLNavigation currentViewController] presentViewController:alerController animated:YES completion:nil];
return nil;
}
@end
//show the alert
[URL_MANAGERN handleURL:@"hz://urlmanger.kit/doAlert?title=alert&message=URL-showAlert" withParams:nil];
导航
//Creates Controller
UIViewController *controller = [UIViewController viewControllerForURL:[NSURL URLWithString:@"hz://page.hz/article"]];
//Gets current Controller
UIViewController *currentViewCtrl = [HZURLNavigation currentViewController];
//Gets current Navigation Controller
UIViewController *currentNavViewCtrl = [HZURLNavigation currentNavigationViewController];
//Dismiss(Pop or dissmiss) View Controller
[HZURLNavigation dismissCurrentAnimated:YES];
参数
@interface UIViewController (HZURLManager)
/**
The URL corresponding to the Controller
*/
@property(nonatomic, strong, readonly) NSString *originURL;
/**
Consists of a query string and additional parameters passed by user.
*/
@property(nonatomic, strong, readonly) NSDictionary *params;
@end