This is a sample router for iOS
To run the example project, clone the repo, and run pod install
from the Example directory first.
iOS 8 and later
ZPMRoute is private cocoapods, To install
add following line in your Podfile at top
source 'http://gitlab.dev.zhaopin.com/Mobile/ios-common-index.git'
pod "ZPMRoute"
ZPMRoute是一个简单的iOS路由组件 详情请移步文章介绍: 移动端路由层设计 一步步构建iOS路由 本代码会随着大家的讨论逐步更新,喜欢的来个星✨~谢谢
注册路由
ZPMRouter *router = [ZPMRouter globalRouter];
[router registerHandler:[[ZPMSignHandler alloc]init] forRoute:@"/signin/:phone([0-9]+)"];
[router registerHandler:[[ZPMUserHandler alloc]init] forRoute:@"/user"];
调用路由
ZPMRouter *router = [ZPMRouter globalRouter];
[self.router handleURL:[NSURL URLWithString:@"/signin/13366376114"] primitiveParameters:@{} targetCallBack:^(NSError * _Nonnull error, id _Nonnull responseObject) {
if (responseObject && [responseObject objectForKey:@"userid"]) {
[SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@", responseObject[@"userid"]]];
}
} withCompletionBlock:^(BOOL handled, NSError * _Nonnull error) {
}];
[self.router handleURL:[NSURL URLWithString:@"ZPMDemo://com.ZPMroute.demo/user"] primitiveParameters:@{@"user":@"Neo~🙃🙃"} targetCallBack:^(NSError *error, id responseObject) {
NSLog(@"UserCallBack %@", responseObject);
} withCompletionBlock:^(BOOL handled, NSError *error) {
NSLog(@"UserHandleCompletion %@", error);
}];
注册路由
ZPMRouter *router = [ZPMRouter globalRouter];
[router registerBlock:^ZPMRouteRequest *(ZPMRouteRequest *request) {
ZPMResumeViewController *vc = [[ZPMResumeViewController alloc] init];
vc.ZPM_request = request;
[(UINavigationController *)self.window.rootViewController pushViewController:vc animated:true];
return request;
} forRoute:@"/foo/var"];
调用路由
ZPMRouter *router = [ZPMRouter globalRouter];
[router handleURL:[NSURL URLWithString:@"/foo/var"] primitiveParameters:nil targetCallBack:^(NSError *err, id responseObject) {
} withCompletionBlock:^(BOOL handled, NSError *error) {
}];
ZPMRouter *router = [ZPMRouter globalRouter];
ZPMRouter *router = [ZPMRouter routerForScheme:@"Resume"];
[router setUnhandledURLHandler:^(ZPMRouter * _Nonnull routes, NSURL * _Nullable URL, NSDictionary<NSString *,id> * _Nullable parameters) {
NSLog(@"%@, %@, %@", routes, URL, parameters);
[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"can not handle the URL %@", URL.absoluteString]];
}];
路由不匹配的降级
router.shouldFallbackGlobalRouter = true;
Neo, 394570610@qq.com
ZPMRoute is available under the MIT license. See the LICENSE file for more info.