JC导航器
是一家分离的iOS开发中模块或应用程序之间跳转的导航器框架。
特性
此框架支持在ARC下开发iOS 8.0+。
- JC导航器配置。
- 实现模块映射。
- 使用openURL: 或 openWithMapKey: 方法进行跳转操作。
- 模块间的参数传递。
JC导航器配置
使用hostList设置URL跳转规则
[[JCNavigator sharedNavigator] addURLScheme:@"joych" hostList:@[@"com.joych.JCNavigatorDemo"]];
设置rootViewController
ViewController *vc = [[ViewController alloc] init];
[[JCNavigator sharedNavigator] setRootViewController:vc];
实现模块映射
JCTestModuleMap类声明为JCModuleMap的子类。
- 映射键应由相同的前缀定义,并附加"_"。
- 映射键将用于与该模块关联的JC导航器类别。
- 实现用于模块间跳转的接口。
// JCTestModuleMap.h
FOUNDATION_EXPORT NSString *const JCFirstLevelMapKey;
FOUNDATION_EXPORT NSString *const JCSecondLevelMapKey;
FOUNDATION_EXPORT NSString *const JCThirdLevelMapKey;
FOUNDATION_EXPORT NSString *const JCContentDetailMapKey;
@interface JCTestModuleMap : JCModuleMap
+ (void)openFirstLevelVCPresented:(BOOL)presented
propertiesDict:(NSDictionary *)propertiesDict;
+ (void)openSecondLevelVCPresented:(BOOL)presented;
+ (void)openThirdLevelVCPresented:(BOOL)presented;
+ (void)openContentDetailViewControllerWithCurrentIndex:(NSString *)currentIndex
testId:(NSString *)testId
testArray:(NSArray *)testArray;
@end
// JCTestModuleMap.m
NSString *const JCFirstLevelMapKey = @"JC_firstLevel";
NSString *const JCSecondLevelMapKey = @"JC_secondLevel";
NSString *const JCThirdLevelMapKey = @"JC_thirdLevel";
NSString *const JCContentDetailMapKey = @"JC_contentDetail";
@implementation JCTestModuleMap
+ (void)initialize
{
[[JCNavigator sharedNavigator] addModuleMap:[JCTestModuleMap new]];
}
+ (void)openFirstLevelVCPresented:(BOOL)presented propertiesDict:(NSDictionary *)propertiesDict
{
if (presented) {
[[JCNavigator sharedNavigator] openWithMapKey:JCFirstLevelMapKey propertiesBlock:^NSDictionary *{
return propertiesDict;
} presented:YES animated:YES];
return;
}
[[JCNavigator sharedNavigator] openWithMapKey:JCFirstLevelMapKey propertiesBlock:^NSDictionary *{
return propertiesDict;
}];
// [[JCNavigator sharedNavigator] openURL:[NSURL URLWithString:@"joych://com.joych.JCNavigatorDemo/firstlevel"]];
// [[JCNavigator sharedNavigator] openURLString:@"joych://com.joych.JCNavigatorDemo/firstlevel"];
}
+ (void)openSecondLevelVCPresented:(BOOL)presented
{
if (presented) {
[[JCNavigator sharedNavigator] openWithMapKey:JCSecondLevelMapKey propertiesBlock:nil presented:YES animated:YES];
return;
}
[[JCNavigator sharedNavigator] openWithMapKey:JCSecondLevelMapKey];
// [[JCNavigator sharedNavigator] openURL:[NSURL URLWithString:@"joych://com.joych.JCNavigatorDemo/secondlevel"]];
// [[JCNavigator sharedNavigator] openURLString:@"joych://com.joych.JCNavigatorDemo/secondlevel"];
}
+ (void)openThirdLevelVCPresented:(BOOL)presented
{
if (presented) {
[[JCNavigator sharedNavigator] openWithMapKey:JCThirdLevelMapKey propertiesBlock:nil presented:YES animated:YES];
return;
}
[[JCNavigator sharedNavigator] openWithMapKey:JCThirdLevelMapKey];
// [[JCNavigator sharedNavigator] openURL:[NSURL URLWithString:@"joych://com.joych.JCNavigatorDemo/thirdlevel"]];
// [[JCNavigator sharedNavigator] openURLString:@"joych://com.joych.JCNavigatorDemo/thirdlevel"];
}
+ (void)openContentDetailViewControllerWithCurrentIndex:(NSString *)currentIndex testId:(NSString *)testId testArray:(NSArray *)testArray
{
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:3];
if ([currentIndex isKindOfClass:[NSString class]]) {
params[@"currentIndex"] = currentIndex;
}
if ([testId isKindOfClass:[NSString class]]) {
params[@"testId"] = testId;
}
if ([testArray isKindOfClass:[NSArray class]]) {
params[@"testArray"] = testArray;
}
[[JCNavigator sharedNavigator] openWithMapKey:JCContentDetailMapKey propertiesBlock:^NSDictionary *{
return params;
} presented:YES animated:YES];
}
- (NSDictionary<NSString *,Class> *)classesForMapKeys
{
return @{JCFirstLevelMapKey: NSClassFromString(@"JCFirstLevelViewController"),
JCSecondLevelMapKey: NSClassFromString(@"JCSecondLevelViewController"),
JCThirdLevelMapKey: NSClassFromString(@"JCThirdLevelViewController"),
JCContentDetailMapKey: NSClassFromString(@"JCContentDetailViewController"),
};
}
- (BOOL)presentedForClass:(Class)viewControllerClass
{
if ([viewControllerClass isEqual:NSClassFromString(@"JCContentDetailViewController")]) {
return YES;
}
return NO;
}
- (NSArray<Class> *)reuseViewControllerClasses
{
return @[NSClassFromString(@"JCFirstLevelViewController")];
}
- (NSDictionary<NSString *,NSDictionary *> *)propertiesMapOfURLQueryForClasses
{
return @{@"JCContentDetailViewController": @{@"pageindex": @"currentIndex"}};
}
@end
使用方法 openURL: 或 openWithMapKey 进行跳转操作
在应用程序或模块间打开 URL。
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
return [[JCNavigator sharedNavigator] openURL:url options:options];
}
[[JCNavigator sharedNavigator] openURL:[NSURL URLWithString:@"joych://com.joych.JCNavigatorDemo/firstlevel"]];
[[JCNavigator sharedNavigator] openURLString:@"joych://com.joych.jcnavigatordemo/contentdetail?pageindex=1"];
模块间参数传递
模块间参数传递是通过属性赋值实现的。
- 建议将属性声明为 NSString 类,因为 openURL: 方法只支持此数据类型。
- 属性也可以声明为 NSArray / NSDictionary / NSSet / UIImage 等数据类型,它们可以与 openWithMapKey:propertiesBlock: 方法一起使用。对于模块间的解耦,尽管可以使用自定义对象,但并不推荐。
// JCContentDetailViewController.h
@protocol JCContentDetailDelegate <NSObject>
@property (nonatomic, strong) NSString *currentIndex;
@property (nonatomic, strong) NSString *testId;
@property (nonatomic, strong) NSArray *testArray;
@end
@interface JCContentDetailViewController : UIViewController<JCContentDetailDelegates>
@end
CocoaPods
要将 JCNavigator 集成到您的 iOS 项目中,请在 Podfile 中指定它
pod 'JCNavigator'
联系人
如果您对框架有任何问题或建议,请通过 E-mail 联系我。
作者: Joych
E-mail: [email protected]
许可证
JCNavigator 采用 MIT 许可证发布。