Skip to content

Commit 1fc3c96

Browse files
committed
Merge pull request #49 from usebutton/wes/scheme_matching
Add scheme matching support
2 parents d3e7dfe + 31344bd commit 1fc3c96

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

DeepLinkSDK/RouteMatcher/DPLRouteMatcher.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
@interface DPLRouteMatcher ()
77

8+
@property (nonatomic, copy) NSString *scheme;
89
@property (nonatomic, strong) DPLRegularExpression *regexMatcher;
910

1011
@end
@@ -23,7 +24,10 @@ - (instancetype)initWithRoute:(NSString *)route {
2324

2425
self = [super init];
2526
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]];
2731
}
2832

2933
return self;
@@ -33,9 +37,11 @@ - (instancetype)initWithRoute:(NSString *)route {
3337
- (DPLDeepLink *)deepLinkWithURL:(NSURL *)url {
3438

3539
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];
3841

42+
if (self.scheme.length && ![self.scheme isEqualToString:deepLink.URL.scheme]) {
43+
return nil;
44+
}
3945

4046
DPLMatchResult *matchResult = [self.regexMatcher matchResultForString:deepLinkString];
4147
if (!matchResult.isMatch) {

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,17 @@ router[@"timeline"] = ^{ … }
110110
111111
```
112112

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:
113114

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+
```
114124
115125
## Running the Demo
116126

Tests/RouteMatcher/DPLRouteMatcherSpec.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,36 @@
186186
});
187187
});
188188

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+
189221
SpecEnd

0 commit comments

Comments
 (0)