Skip to content

Commit

Permalink
本次提交包含以下内容:
Browse files Browse the repository at this point in the history
功能更新及优化:
2.0.0 警告处理
  • Loading branch information
LittleJamie committed Feb 4, 2021
1 parent aa3c089 commit 21baba3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 40 deletions.
2 changes: 1 addition & 1 deletion DNRouter.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'DNRouter'
s.version = '1.5.1'
s.version = '2.0.0'
s.summary = 'A short description of DNRouter.'

# This description is used to generate tags and improve search results.
Expand Down
12 changes: 6 additions & 6 deletions DNRouter/Classes/DNRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ extern NSString *const DNRouterParameterCompletion;
extern NSString *const DNRouterParameterUserInfo;

/// routerParameters内置的几个参数会需要配合上面定义的string使用
typedef void(^DNRouterHandler)(NSDictionary *routerParameters);
typedef void(^DNRouterHandler)(NSDictionary * __nullable routerParameters);

/// 需要返回一个 object,配合 objectForURL: 使用
typedef id _Nullable (^DNRouterObjectHandler)(NSDictionary *routerParameters);
typedef id _Nullable (^DNRouterObjectHandler)(NSDictionary * __nullable routerParameters);
//
/// 需要返回一个 object,配合 objectForURL: 使用
typedef void(^DNRouterCompletion)(id __nullable result);
Expand All @@ -32,12 +32,12 @@ typedef void(^DNRouterCompletion)(id __nullable result);
/// 注册 URLPattern 对应的 Handler,在 handler 中可以初始化 VC,然后对 VC 做各种操作
/// @param URLPattern URL键值;带上 scheme,如 renren://beauty/:id
/// @param handler 该 block 会传一个字典,包含了注册的 URL 中对应的变量。假如注册的 URL 为 renren://beauty/:id 那么,就会传一个 @{@"id": 4} 这样的字典过来
+ (void)registerURLPattern:(NSString *__nullable)URLPattern toHandler:(DNRouterHandler)handler;
+ (void)registerURLPattern:(NSString *__nullable)URLPattern toHandler:(DNRouterHandler __nullable)handler;

/// 注册 URLPattern 对应的 ObjectHandler,需要返回一个 object 给调用方
/// @param URLPattern URL键值;带上 scheme,如 mgj://beauty/:id
/// @param handler 该 block 会传一个字典,包含了注册的 URL 中对应的变量。假如注册的 URL 为 renren://beauty/:id 那么,就会传一个 @{@"id": 4} 这样的字典过来,自带的 key 为 @"url" 和 @"completion" (如果有的话)
+ (void)registerURLPattern:(NSString *__nullable)URLPattern toObjectHandler:(DNRouterObjectHandler)handler;
+ (void)registerURLPattern:(NSString *__nullable)URLPattern toObjectHandler:(DNRouterObjectHandler __nullable)handler;

#pragma mark - Cancel
/// 取消注册某个 URL Pattern
Expand All @@ -55,14 +55,14 @@ typedef void(^DNRouterCompletion)(id __nullable result);
/// 打开此 URL,同时当操作完成时,执行额外的代码
/// @param URL 带 Scheme 的 URL,如 renren://beauty/4
/// @param completion URL 处理完成后的 callback,完成的判定跟具体的业务相关
+ (void)openURL:(NSString *__nullable)URL completion:(DNRouterCompletion)completion;
+ (void)openURL:(NSString *__nullable)URL completion:(DNRouterCompletion __nullable)completion;

/// 打开此 URL,带上附加信息,同时当操作完成时,执行额外的代码

/// @param URL 带 Scheme 的 URL,如 renren://beauty/4
/// @param userInfo 附加参数
/// @param completion URL 处理完成后的 callback,完成的判定跟具体的业务相关
+ (void)openURL:(NSString *__nullable)URL withUserInfo:(NSDictionary *__nullable)userInfo completion:(DNRouterCompletion)completion;
+ (void)openURL:(NSString *__nullable)URL withUserInfo:(NSDictionary *__nullable)userInfo completion:(DNRouterCompletion __nullable)completion;

#pragma mark - Find

Expand Down
49 changes: 32 additions & 17 deletions DNRouter/Classes/DNRouter.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,41 @@ + (instancetype)sharedInstance
}

#pragma mark - Public
+ (void)registerURLPattern:(NSString *__nullable)URLPattern toHandler:(DNRouterHandler)handler {
+ (void)registerURLPattern:(NSString *__nullable)URLPattern toHandler:(DNRouterHandler __nullable)handler {
[[self sharedInstance] addURLPattern:URLPattern andHandler:handler];
}

+ (void)registerURLPattern:(NSString *__nullable)URLPattern toObjectHandler:(DNRouterObjectHandler)handler {
[[self sharedInstance] addURLPattern:URLPattern andObjectHandler:handler];
+ (void)registerURLPattern:(NSString *__nullable)URLPattern toObjectHandler:(DNRouterObjectHandler __nullable)handler {
[[self sharedInstance] addURLPattern:URLPattern andObjectHandler:handler];
}

+ (void)deregisterURLPattern:(NSString *)URLPattern {
+ (void)deregisterURLPattern:(NSString * __nullable)URLPattern {
[[self sharedInstance] removeURLPattern:URLPattern];
}

+ (void)openURL:(NSString *__nullable)URL {
[self openURL:URL completion:nil];
}

+ (void)openURL:(NSString *__nullable)URL completion:(DNRouterCompletion)completion {
+ (void)openURL:(NSString *__nullable)URL completion:(DNRouterCompletion __nullable)completion {
[self openURL:URL withUserInfo:nil completion:completion];
}

+ (void)openURL:(NSString *__nullable)URL withUserInfo:(NSDictionary *)userInfo completion:(DNRouterCompletion) completion {
+ (void)openURL:(NSString *__nullable)URL withUserInfo:(NSDictionary *)userInfo completion:(DNRouterCompletion __nullable)completion {

// if (@available(iOS 9.0, *)) {
// NSString *charactersToEscape = @"?!@#$^&%*+,;='\"`<>()[]{}\\| ";
// NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
// URL = [URL stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
// } else {
//
// }
URL = [URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *parameters = [[self sharedInstance] extractParametersFromURL:URL];

[parameters enumerateKeysAndObjectsUsingBlock:^(id key, NSString *obj, BOOL *stop) {
if ([obj isKindOfClass:[NSString class]]) {
parameters[key] = [obj stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
parameters[key] = obj.stringByRemovingPercentEncoding;
}
}];

Expand All @@ -87,13 +95,20 @@ + (void)openURL:(NSString *__nullable)URL withUserInfo:(NSDictionary *)userInfo
}
}

+ (id)objectForURL:(NSString *)URL {
+ (id)objectForURL:(NSString * __nullable)URL {
return [self objectForURL:URL withUserInfo:nil];
}

+ (id)objectForURL:(NSString *)URL withUserInfo:(NSDictionary *)userInfo {
+ (id)objectForURL:(NSString * __nullable)URL withUserInfo:(NSDictionary * __nullable)userInfo {
DNRouter *router = [DNRouter sharedInstance];

// if (@available(iOS 9.0, *)) {
// NSString *charactersToEscape = @"?!@#$^&%*+,;='\"`<>()[]{}\\| ";
// NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
// URL = [URL stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
// } else {
//
// }
URL = [URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *parameters = [router extractParametersFromURL:URL];
DNRouterObjectHandler handler = parameters[@"block"];
Expand All @@ -108,11 +123,11 @@ + (id)objectForURL:(NSString *)URL withUserInfo:(NSDictionary *)userInfo {
return nil;
}

+ (BOOL)canOpenURL:(NSString *)URL {
+ (BOOL)canOpenURL:(NSString * __nullable)URL {
return [[self sharedInstance] extractParametersFromURL:URL] ? YES : NO;
}

+ (NSString *)generateURLWithPattern:(NSString *)pattern parameters:(NSArray *)parameters {
+ (NSString *)generateURLWithPattern:(NSString * __nullable)pattern parameters:(NSArray * __nullable)parameters {
NSInteger startIndexOfColon = 0;

NSMutableArray *placeholders = [NSMutableArray array];
Expand Down Expand Up @@ -150,14 +165,14 @@ + (NSString *)generateURLWithPattern:(NSString *)pattern parameters:(NSArray *)p
}

#pragma mark - Private
- (void)addURLPattern:(NSString *)URLPattern andHandler:(DNRouterHandler)handler{
- (void)addURLPattern:(NSString * __nullable)URLPattern andHandler:(DNRouterHandler __nullable)handler{
NSMutableDictionary *subRoutes = [self addURLPattern:URLPattern];
if (handler && subRoutes) {
subRoutes[@"_"] = [handler copy];
}
}

- (NSMutableDictionary *)addURLPattern:(NSString *)URLPattern
- (NSMutableDictionary *)addURLPattern:(NSString * __nullable)URLPattern
{
NSArray *pathComponents = [self pathComponentsFromURL:URLPattern];

Expand All @@ -172,7 +187,7 @@ - (NSMutableDictionary *)addURLPattern:(NSString *)URLPattern
return subRoutes;
}

- (NSArray*)pathComponentsFromURL:(NSString*)URL
- (NSArray*)pathComponentsFromURL:(NSString * __nullable)URL
{
NSMutableArray *pathComponents = [NSMutableArray array];
if ([URL rangeOfString:@"://"].location != NSNotFound) {
Expand All @@ -196,7 +211,7 @@ - (NSArray*)pathComponentsFromURL:(NSString*)URL
return [pathComponents copy];
}

- (void)addURLPattern:(NSString *)URLPattern andObjectHandler:(DNRouterObjectHandler)handler
- (void)addURLPattern:(NSString * __nullable)URLPattern andObjectHandler:(DNRouterObjectHandler __nullable)handler
{
NSMutableDictionary *subRoutes = [self addURLPattern:URLPattern];
if (handler && subRoutes) {
Expand Down Expand Up @@ -229,7 +244,7 @@ - (void)removeURLPattern:(NSString *)URLPattern
}
}

- (NSMutableDictionary *)extractParametersFromURL:(NSString *)url
- (NSMutableDictionary *)extractParametersFromURL:(NSString * __nullable)url
{
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];

Expand Down Expand Up @@ -309,7 +324,7 @@ - (NSMutableDictionary *)routes
return _routes;
}

+ (BOOL)checkIfContainsSpecialCharacter:(NSString *)checkedString {
+ (BOOL)checkIfContainsSpecialCharacter:(NSString * __nullable)checkedString {
NSCharacterSet *specialCharactersSet = [NSCharacterSet characterSetWithCharactersInString:specialCharacters];
return [checkedString rangeOfCharacterFromSet:specialCharactersSet].location != NSNotFound;
}
Expand Down
16 changes: 10 additions & 6 deletions Example/DNRouter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@
isa = PBXProject;
attributes = {
CLASSPREFIX = DN;
LastUpgradeCheck = 1120;
LastUpgradeCheck = 1240;
ORGANIZATIONNAME = "418589912@qq.com";
TargetAttributes = {
6003F589195388D20070C39A = {
DevelopmentTeam = W2A55P52D2;
DevelopmentTeam = VKZ5S4BDQY;
};
6003F5AD195388D20070C39A = {
TestTargetID = 6003F589195388D20070C39A;
Expand Down Expand Up @@ -475,6 +475,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand All @@ -499,7 +500,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down Expand Up @@ -529,6 +530,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand All @@ -546,7 +548,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
Expand All @@ -558,10 +560,11 @@
baseConfigurationReference = 55A37A01DF88BDA0CFA91800 /* Pods-DNRouter_Example.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = W2A55P52D2;
DEVELOPMENT_TEAM = VKZ5S4BDQY;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "DNRouter/DNRouter-Prefix.pch";
INFOPLIST_FILE = "DNRouter/DNRouter-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -574,10 +577,11 @@
baseConfigurationReference = F8B154ED6EC0FD393C1C2500 /* Pods-DNRouter_Example.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = W2A55P52D2;
DEVELOPMENT_TEAM = VKZ5S4BDQY;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "DNRouter/DNRouter-Prefix.pch";
INFOPLIST_FILE = "DNRouter/DNRouter-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1120"
LastUpgradeVersion = "1240"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
20 changes: 11 additions & 9 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 21baba3

Please sign in to comment.