File tree Expand file tree Collapse file tree 3 files changed +51
-3
lines changed Expand file tree Collapse file tree 3 files changed +51
-3
lines changed Original file line number Diff line number Diff line change 5
5
6
6
@interface DPLRouteMatcher ()
7
7
8
+ @property (nonatomic , copy ) NSString *scheme;
8
9
@property (nonatomic , strong ) DPLRegularExpression *regexMatcher;
9
10
10
11
@end
@@ -23,7 +24,10 @@ - (instancetype)initWithRoute:(NSString *)route {
23
24
24
25
self = [super init ];
25
26
if (self) {
26
- _regexMatcher = [DPLRegularExpression regularExpressionWithPattern: route];
27
+
28
+ NSArray *parts = [route componentsSeparatedByString: @" ://" ];
29
+ _scheme = parts.count > 1 ? [parts firstObject ] : nil ;
30
+ _regexMatcher = [DPLRegularExpression regularExpressionWithPattern: [parts lastObject ]];
27
31
}
28
32
29
33
return self;
@@ -33,9 +37,11 @@ - (instancetype)initWithRoute:(NSString *)route {
33
37
- (DPLDeepLink *)deepLinkWithURL : (NSURL *)url {
34
38
35
39
DPLDeepLink *deepLink = [[DPLDeepLink alloc ] initWithURL: url];
36
- NSString *deepLinkString = [NSString stringWithFormat: @" %@%@ " ,
37
- deepLink.URL.host, deepLink.URL.path];
40
+ NSString *deepLinkString = [NSString stringWithFormat: @" %@%@ " , deepLink.URL.host, deepLink.URL.path];
38
41
42
+ if (self.scheme .length && ![self .scheme isEqualToString: deepLink.URL.scheme]) {
43
+ return nil ;
44
+ }
39
45
40
46
DPLMatchResult *matchResult = [self .regexMatcher matchResultForString: deepLinkString];
41
47
if (!matchResult.isMatch ) {
Original file line number Diff line number Diff line change @@ -110,7 +110,17 @@ router[@"timeline"] = ^{ … }
110
110
111
111
```
112
112
113
+ You can also be scheme specific. If you support multiple URL schemes in your app, you can register routes specific to those schemes as follows:
113
114
115
+ An incoming URL of ` scheme-one://timeline `
116
+
117
+ ``` objc
118
+ // Matches the URL.
119
+ router[@" scheme-one://timeline" ] = ^{ … }
120
+
121
+ // Does not match the URL.
122
+ router[ @"scheme-two://timeline"] = ^{ … }
123
+ ```
114
124
115
125
## Running the Demo
116
126
Original file line number Diff line number Diff line change 186
186
});
187
187
});
188
188
189
+
190
+ describe (@" Matching on Schemes" , ^{
191
+
192
+ __block NSURL *url1;
193
+ __block NSURL *url2;
194
+ beforeEach (^{
195
+ url1 = [NSURL URLWithString: @" derp://dpl.io/say/hello" ];
196
+ url2 = [NSURL URLWithString: @" foo://dpl.io/say/hello" ];
197
+ });
198
+
199
+ it (@" allows any scheme if not specified in the route" , ^{
200
+ DPLRouteMatcher *matcher = [DPLRouteMatcher matcherWithRoute: @" /say/hello" ];
201
+ DPLDeepLink *deepLink = [matcher deepLinkWithURL: url1];
202
+ expect (deepLink).toNot .beNil ();
203
+
204
+ deepLink = [matcher deepLinkWithURL: url2];
205
+ expect (deepLink).toNot .beNil ();
206
+ });
207
+
208
+ it (@" matches a url with a scheme specific route" , ^{
209
+ DPLRouteMatcher *matcher = [DPLRouteMatcher matcherWithRoute: @" derp://(.*)/say/hello" ];
210
+ DPLDeepLink *deepLink = [matcher deepLinkWithURL: url1];
211
+ expect (deepLink).toNot .beNil ();
212
+ });
213
+
214
+ it (@" does NOT match a url with a different scheme than the route" , ^{
215
+ DPLRouteMatcher *matcher = [DPLRouteMatcher matcherWithRoute: @" derp://(.*)/say/hello" ];
216
+ DPLDeepLink *deepLink = [matcher deepLinkWithURL: url2];
217
+ expect (deepLink).to .beNil ();
218
+ });
219
+ });
220
+
189
221
SpecEnd
You can’t perform that action at this time.
0 commit comments