YHLinkDefaultParser.m
5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// YHLinkDefaultParser.m
// YohoExplorerDemo
//
// Created by gaoqiang xu on 4/21/15.
// Copyright (c) 2015 gaoqiang xu. All rights reserved.
//
#import "YHLinkDefaultParser.h"
NSString * const YHLinkParseResult_FreshUrlKey = @"YHLinkParseResult_FreshUrl";
NSString * const YHLinkParseResult_OpenByKey = @"YHLinkParseResult_OpenBy";
NSString * const YHLinkParseResult_OpenParametersKey = @"YHLinkParseResult_OpenParameters";
NSString * const YHLinkParseResult_OriginalUrlKey = @"YHLinkParseResult_OriginalUrl";
@implementation YHLinkDefaultParser
@synthesize completion = _completion;
- (NSString *)leadingString
{
return @"openby:";
}
- (BOOL)parseString:(NSString *)string
{
if (string.length == 0) {
return NO;
}
BOOL b = NO;
NSString *freshUrl = @"";
NSString *contentString = string;
NSString *begin = [self leadingString];
NSRange range = [contentString rangeOfString:begin];
if (range.location != NSNotFound) {
contentString = [contentString substringFromIndex:range.location+range.length];
freshUrl = [string substringToIndex:range.location];
if (freshUrl.length > 0
&& [[freshUrl substringWithRange:NSMakeRange(freshUrl.length-1, 1)] isEqualToString:@"?"]) {
freshUrl = [freshUrl substringToIndex:freshUrl.length-1];
}
} else {
return b;
}
if (contentString.length == 0) {
return b;
}
contentString = [contentString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSArray *comps = nil;
NSRange rangeOfEqual = [contentString rangeOfString:@"="];
if (rangeOfEqual.location != NSNotFound) {
comps = @[ [contentString substringToIndex:rangeOfEqual.location],
[contentString substringFromIndex:rangeOfEqual.location+rangeOfEqual.length] ];
} else {// 兼容全角的"="
rangeOfEqual = [contentString rangeOfString:@"="];
if (rangeOfEqual.location != NSNotFound) {
comps = @[ [contentString substringToIndex:rangeOfEqual.location],
[contentString substringFromIndex:rangeOfEqual.location+rangeOfEqual.length] ];
}
}
if ([comps[0] isEqualToString:YHLinkOpenByType_yohobuy]
|| [comps[0] isEqualToString:YHLinkOpenByType_show]
|| [comps[0] isEqualToString:YHLinkOpenByType_boy]
|| [comps[0] isEqualToString:YHLinkOpenByType_girl]
|| [comps[0] isEqualToString:YHLinkOpenByType_browser]
|| [comps[0] isEqualToString:YHLinkOpenByType_mars]) {
NSString *comp1 = comps[0];
NSError *error = nil;
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:[comps.lastObject dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&error];
if (error || !jsonDic) {
b = NO;
} else {
b = YES;
!self.completion?:self.completion(@{ YHLinkParseResult_OpenByKey:comp1,
YHLinkParseResult_OpenParametersKey:jsonDic,
YHLinkParseResult_FreshUrlKey: freshUrl,
YHLinkParseResult_OriginalUrlKey:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] });
}
}
return b;
}
@end
#pragma mark - YHLinkParserOld
@implementation YHLinkParserOld
@synthesize completion = _completion;
- (NSString *)leadingString
{
return @"app:";
}
- (BOOL)parseString:(NSString *)string
{
if (string.length == 0) {
return NO;
}
BOOL b = NO;
NSString *freshUrl = @"";
NSString *contentString = string;
NSString *begin = [self leadingString];
NSRange range = [contentString rangeOfString:begin];
if (range.location != NSNotFound) {
contentString = [contentString substringFromIndex:range.location+range.length];
freshUrl = [string substringToIndex:range.location];
if ([[freshUrl substringWithRange:NSMakeRange(freshUrl.length-1, 1)] isEqualToString:@"?"]) {
freshUrl = [freshUrl substringToIndex:freshUrl.length-1];
}
} else {
return b;
}
if (contentString.length == 0) {
return b;
}
contentString = [contentString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSArray *comps = [contentString componentsSeparatedByString:@","];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:1];
for (NSString *kv in comps) {
NSRange equalRange = [kv rangeOfString:@"="];
if (equalRange.location == NSNotFound) {
continue;
}
NSString *key = [kv substringToIndex:equalRange.location];
NSString *value = [kv substringFromIndex:equalRange.location+1];
dic[key] = value;
}
if ([dic count] > 0) {
b = YES;
!self.completion?:self.completion(@{ YHLinkParseResult_OpenByKey:YHLinkOpenByType_compatible,
YHLinkParseResult_OpenParametersKey:dic,
YHLinkParseResult_FreshUrlKey: freshUrl,
YHLinkParseResult_OriginalUrlKey:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] });
}
return b;
}
@end