Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
GMRouter
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gengmeiios
GMRouter
Commits
cc32b85d
Commit
cc32b85d
authored
Aug 10, 2020
by
jz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modify router
parent
c0f1d04b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
64 deletions
+68
-64
GMRouter+gm.h
GMRouter/Classes/GMRouter+gm.h
+1
-1
GMRouter+gm.m
GMRouter/Classes/GMRouter+gm.m
+67
-63
No files found.
GMRouter/Classes/GMRouter+gm.h
View file @
cc32b85d
...
@@ -41,7 +41,7 @@ extern NSString *const GMRouterProtocolPrefix;
...
@@ -41,7 +41,7 @@ extern NSString *const GMRouterProtocolPrefix;
* {@"service_id": @"5930",@"is_new_special": @0}
* {@"service_id": @"5930",@"is_new_special": @0}
* @return vc的实例
* @return vc的实例
*/
*/
-
(
id
)
pushScheme
:(
NSString
*
)
urlScheme
params
:(
NSDictionary
*
)
params
;
-
(
id
)
pushScheme
:(
NSString
*
)
urlScheme
params
:(
NSDictionary
*
)
params
completeBlock
:(
GMRouterBlock
)
routerBlock
;
/**
/**
* 初始化Map
* 初始化Map
...
...
GMRouter/Classes/GMRouter+gm.m
View file @
cc32b85d
...
@@ -35,46 +35,8 @@ static NSMutableDictionary *routeMap = nil;
...
@@ -35,46 +35,8 @@ static NSMutableDictionary *routeMap = nil;
-
(
void
)
initializeRouteMap
{
-
(
void
)
initializeRouteMap
{
routeMap
=
[[
NSMutableDictionary
alloc
]
initWithCapacity
:
50
];
routeMap
=
[[
NSMutableDictionary
alloc
]
initWithCapacity
:
50
];
self
.
targets
=
[
NSMutableArray
arrayWithObjects
:
GMRouterTargetAI
,
GMRouterTargetBanking
,
GMRouterTargetCommunity
,
GMRouterTargetWeb
,
nil
];
self
.
targets
=
[
NSMutableArray
arrayWithObjects
:
GMRouterTargetAI
,
GMRouterTargetBanking
,
GMRouterTargetCommunity
,
GMRouterTargetWeb
,
nil
];
}
}
#pragma mark - 获取类的所有方法
// 获取所有的方法
-
(
NSDictionary
*
)
getMethods
:
(
NSString
*
)
clsStr
{
Class
cls
=
NSClassFromString
(
clsStr
);
NSRange
range
=
[
clsStr
rangeOfString
:
@"Target_"
];
NSString
*
targetValue
=
[
clsStr
substringFromIndex
:
range
.
length
];
NSAssert
(
targetValue
.
length
!=
0
,
@"Target_后不能为空!请注意Target"
);
unsigned
int
count
=
0
;
NSMutableDictionary
*
dict
=
[
NSMutableDictionary
dictionary
];
// 获取类的所有 Method
Method
*
methods
=
class_copyMethodList
([
cls
class
],
&
count
);
for
(
NSUInteger
i
=
0
;
i
<
count
;
i
++
)
{
// 获取方法 Name
SEL
methodSEL
=
method_getName
(
methods
[
i
]);
const
char
*
methodName
=
sel_getName
(
methodSEL
);
NSString
*
name
=
[
NSString
stringWithUTF8String
:
methodName
];
//获取到的是这样的 pushToHospitalDetail: 因此要去掉:
NSString
*
rangeStr
=
@":"
;
if
([
name
containsString
:
rangeStr
])
{
NSRange
range
=
[
name
rangeOfString
:
rangeStr
];
name
=
[
name
substringToIndex
:
range
.
location
];
}
NSString
*
promoteStr
=
[
NSString
stringWithFormat
:
@"%@-内有重复的方法名-%@"
,
clsStr
,
name
];
NSAssert
(
!
[
dict
.
allKeys
containsObject
:
name
],
promoteStr
);
//因为消息发送的时候会有两个默认的参数(消息接收者和方法名),所以需要减去2
dict
[
name
]
=
targetValue
;
}
free
(
methods
);
return
dict
;
}
-
(
id
)
pushScheme
:
(
NSString
*
)
urlScheme
{
-
(
id
)
pushScheme
:
(
NSString
*
)
urlScheme
{
return
[
self
pushScheme
:
urlScheme
completeBlock
:
NULL
];
return
[
self
pushScheme
:
urlScheme
completeBlock
:
NULL
];
...
@@ -87,16 +49,24 @@ static NSMutableDictionary *routeMap = nil;
...
@@ -87,16 +49,24 @@ static NSMutableDictionary *routeMap = nil;
}
else
{
}
else
{
encodeUrlScheme
=
[
NSString
stringWithFormat
:
@"%@%@"
,
GMRouterProtocolPrefix
,
urlScheme
];
encodeUrlScheme
=
[
NSString
stringWithFormat
:
@"%@%@"
,
GMRouterProtocolPrefix
,
urlScheme
];
}
}
NSURL
*
url
=
[
NSURL
URLWithString
:
encodeUrlScheme
];
if
(
!
url
)
{
// debugLog(@"协议出错了!");
}
NSString
*
host
=
url
.
host
;
NSString
*
targetName
=
[
routeMap
objectForKey
:
host
];
NSDictionary
*
params
=
[
self
getParams
:
encodeUrlScheme
withHost
:
host
];
host
=
[
self
getHostWithEncodeUrlScheme
:
encodeUrlScheme
host
:
host
];
NSDictionary
*
params
=
[
self
getParams
:
encodeUrlScheme
];
return
[
self
pushScheme
:
urlScheme
params
:
params
completeBlock
:
routerBlock
];
}
-
(
id
)
pushScheme
:
(
NSString
*
)
urlScheme
params
:
(
NSDictionary
*
)
params
completeBlock
:
(
GMRouterBlock
)
routerBlock
{
NSMutableDictionary
*
allParams
=
[
NSMutableDictionary
dictionaryWithDictionary
:
params
];
NSString
*
encodeUrlScheme
=
[
self
URLEncodeString
:
urlScheme
];
NSString
*
host
=
[
self
getHost
:
urlScheme
];
NSString
*
targetName
=
[
routeMap
objectForKey
:
host
];
NSDictionary
*
paramsDict
=
[
self
getParams
:
encodeUrlScheme
];
[
allParams
addEntriesFromDictionary
:
paramsDict
];
host
=
[
self
safeHostWithEncodeUrlScheme
:
encodeUrlScheme
host
:
host
];
id
vc
=
[
self
performTarget
:
targetName
action
:
host
params
:
params
shouldCacheTarget
:
NO
];
id
vc
=
[
self
performTarget
:
targetName
action
:
host
params
:
params
shouldCacheTarget
:
NO
];
//如果是push 过来的 或者需要回调的在此赋值
if
([
vc
isKindOfClass
:[
UIViewController
class
]])
{
if
([
vc
isKindOfClass
:[
UIViewController
class
]])
{
UIViewController
*
VC
=
(
UIViewController
*
)
vc
;
UIViewController
*
VC
=
(
UIViewController
*
)
vc
;
[
self
vcAutoIsPushWith
:
vc
params
:
params
];
[
self
vcAutoIsPushWith
:
vc
params
:
params
];
...
@@ -105,9 +75,19 @@ static NSMutableDictionary *routeMap = nil;
...
@@ -105,9 +75,19 @@ static NSMutableDictionary *routeMap = nil;
return
vc
;
return
vc
;
}
}
// 获取host
-
(
NSString
*
)
getHost
:
(
NSString
*
)
encodeUrlScheme
{
NSURL
*
url
=
[
NSURL
URLWithString
:
encodeUrlScheme
];
if
(
!
url
)
{
// debugLog(@"协议出错了!");
}
return
url
.
host
;
}
#pragma mark - 处理协议后面待的参数
#pragma mark - 处理协议后面待的参数
-
(
NSDictionary
*
)
getParams
:
(
NSString
*
)
encodeUrlScheme
withHost
:
(
NSString
*
)
host
{
-
(
NSDictionary
*
)
getParams
:
(
NSString
*
)
encodeUrlScheme
{
NSString
*
host
=
[
self
getHost
:
encodeUrlScheme
];
NSDictionary
*
params
;
NSDictionary
*
params
;
NSArray
*
array
=
[
encodeUrlScheme
componentsSeparatedByString
:
@"url="
];
NSArray
*
array
=
[
encodeUrlScheme
componentsSeparatedByString
:
@"url="
];
if
(([
host
isEqualToString
:
@"third_webview"
]
||
[
host
isEqualToString
:
@"common_webview"
])
&&
array
.
count
>
1
)
{
if
(([
host
isEqualToString
:
@"third_webview"
]
||
[
host
isEqualToString
:
@"common_webview"
])
&&
array
.
count
>
1
)
{
...
@@ -125,7 +105,8 @@ static NSMutableDictionary *routeMap = nil;
...
@@ -125,7 +105,8 @@ static NSMutableDictionary *routeMap = nil;
return
params
;
return
params
;
}
}
-
(
NSString
*
)
getHostWithEncodeUrlScheme
:
(
NSString
*
)
encodeUrlScheme
host
:
(
NSString
*
)
host
{
// 如果不在白名单的三方url 禁止获取我们APP内重要信息
-
(
NSString
*
)
safeHostWithEncodeUrlScheme
:
(
NSString
*
)
encodeUrlScheme
host
:
(
NSString
*
)
host
{
NSArray
*
array
=
[
encodeUrlScheme
componentsSeparatedByString
:
@"url="
];
NSArray
*
array
=
[
encodeUrlScheme
componentsSeparatedByString
:
@"url="
];
if
(([
host
isEqualToString
:
@"third_webview"
]
||
[
host
isEqualToString
:
@"common_webview"
])
&&
array
.
count
>
1
)
{
if
(([
host
isEqualToString
:
@"third_webview"
]
||
[
host
isEqualToString
:
@"common_webview"
])
&&
array
.
count
>
1
)
{
NSString
*
value
=
array
[
1
];
NSString
*
value
=
array
[
1
];
...
@@ -146,21 +127,6 @@ static NSMutableDictionary *routeMap = nil;
...
@@ -146,21 +127,6 @@ static NSMutableDictionary *routeMap = nil;
return
host
;
return
host
;
}
}
-
(
id
)
pushScheme
:
(
NSString
*
)
urlScheme
params
:
(
NSDictionary
*
)
params
{
NSMutableDictionary
*
allParams
=
[
NSMutableDictionary
dictionaryWithDictionary
:
params
];
NSString
*
encodeUrlScheme
=
[
self
URLEncodeString
:
urlScheme
];
NSURL
*
url
=
[
NSURL
URLWithString
:
encodeUrlScheme
];
if
(
!
url
)
{
// debugLog(@"协议出错了!");
}
NSString
*
host
=
url
.
host
;
NSString
*
targetName
=
[
routeMap
objectForKey
:
host
];
NSDictionary
*
paramsDict
=
[
self
getParams
:
encodeUrlScheme
withHost
:
host
];
[
allParams
addEntriesFromDictionary
:
paramsDict
];
host
=
[
self
getHostWithEncodeUrlScheme
:
encodeUrlScheme
host
:
host
];
return
[
self
performTarget
:
targetName
action
:
host
params
:
allParams
shouldCacheTarget
:
NO
];
}
#pragma mark - autoIsPush
#pragma mark - autoIsPush
-
(
void
)
vcAutoIsPushWith
:
(
UIViewController
*
)
vc
params
:
(
NSDictionary
*
)
params
{
-
(
void
)
vcAutoIsPushWith
:
(
UIViewController
*
)
vc
params
:
(
NSDictionary
*
)
params
{
if
(
vc
!=
nil
&&
([
params
.
allKeys
containsObject
:
@"ispush"
]))
{
if
(
vc
!=
nil
&&
([
params
.
allKeys
containsObject
:
@"ispush"
]))
{
...
@@ -236,4 +202,42 @@ static NSMutableDictionary *routeMap = nil;
...
@@ -236,4 +202,42 @@ static NSMutableDictionary *routeMap = nil;
[
routeMap
addEntriesFromDictionary
:
dict
];
[
routeMap
addEntriesFromDictionary
:
dict
];
}
}
}
}
#pragma mark - 获取类的所有方法
// 获取所有的方法
-
(
NSDictionary
*
)
getMethods
:
(
NSString
*
)
clsStr
{
Class
cls
=
NSClassFromString
(
clsStr
);
NSRange
range
=
[
clsStr
rangeOfString
:
@"Target_"
];
NSString
*
targetValue
=
[
clsStr
substringFromIndex
:
range
.
length
];
NSAssert
(
targetValue
.
length
!=
0
,
@"Target_后不能为空!请注意Target"
);
unsigned
int
count
=
0
;
NSMutableDictionary
*
dict
=
[
NSMutableDictionary
dictionary
];
// 获取类的所有 Method
Method
*
methods
=
class_copyMethodList
([
cls
class
],
&
count
);
for
(
NSUInteger
i
=
0
;
i
<
count
;
i
++
)
{
// 获取方法 Name
SEL
methodSEL
=
method_getName
(
methods
[
i
]);
const
char
*
methodName
=
sel_getName
(
methodSEL
);
NSString
*
name
=
[
NSString
stringWithUTF8String
:
methodName
];
//获取到的是这样的 pushToHospitalDetail: 因此要去掉:
NSString
*
rangeStr
=
@":"
;
if
([
name
containsString
:
rangeStr
])
{
NSRange
range
=
[
name
rangeOfString
:
rangeStr
];
name
=
[
name
substringToIndex
:
range
.
location
];
}
NSString
*
promoteStr
=
[
NSString
stringWithFormat
:
@"%@-内有重复的方法名-%@"
,
clsStr
,
name
];
NSAssert
(
!
[
dict
.
allKeys
containsObject
:
name
],
promoteStr
);
//因为消息发送的时候会有两个默认的参数(消息接收者和方法名),所以需要减去2
dict
[
name
]
=
targetValue
;
}
free
(
methods
);
return
dict
;
}
@end
@end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment