From 6ea872f7d57e7000f478a61f56c498acde8c15b4 Mon Sep 17 00:00:00 2001 From: Wes Smith Date: Wed, 13 May 2015 13:40:23 -0400 Subject: [PATCH 1/2] add scheme matching support --- DeepLinkSDK/RouteMatcher/DPLRouteMatcher.m | 12 ++++++-- Tests/RouteMatcher/DPLRouteMatcherSpec.m | 32 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/DeepLinkSDK/RouteMatcher/DPLRouteMatcher.m b/DeepLinkSDK/RouteMatcher/DPLRouteMatcher.m index f325abe..188a2ac 100644 --- a/DeepLinkSDK/RouteMatcher/DPLRouteMatcher.m +++ b/DeepLinkSDK/RouteMatcher/DPLRouteMatcher.m @@ -5,6 +5,7 @@ @interface DPLRouteMatcher () +@property (nonatomic, copy) NSString *scheme; @property (nonatomic, strong) DPLRegularExpression *regexMatcher; @end @@ -23,7 +24,10 @@ - (instancetype)initWithRoute:(NSString *)route { self = [super init]; if (self) { - _regexMatcher = [DPLRegularExpression regularExpressionWithPattern:route]; + + NSArray *parts = [route componentsSeparatedByString:@"://"]; + _scheme = parts.count > 1 ? [parts firstObject] : nil; + _regexMatcher = [DPLRegularExpression regularExpressionWithPattern:[parts lastObject]]; } return self; @@ -33,9 +37,11 @@ - (instancetype)initWithRoute:(NSString *)route { - (DPLDeepLink *)deepLinkWithURL:(NSURL *)url { DPLDeepLink *deepLink = [[DPLDeepLink alloc] initWithURL:url]; - NSString *deepLinkString = [NSString stringWithFormat:@"%@%@", - deepLink.URL.host, deepLink.URL.path]; + NSString *deepLinkString = [NSString stringWithFormat:@"%@%@", deepLink.URL.host, deepLink.URL.path]; + if (self.scheme.length && ![self.scheme isEqualToString:deepLink.URL.scheme]) { + return nil; + } DPLMatchResult *matchResult = [self.regexMatcher matchResultForString:deepLinkString]; if (!matchResult.isMatch) { diff --git a/Tests/RouteMatcher/DPLRouteMatcherSpec.m b/Tests/RouteMatcher/DPLRouteMatcherSpec.m index 9068f19..a26f2ea 100644 --- a/Tests/RouteMatcher/DPLRouteMatcherSpec.m +++ b/Tests/RouteMatcher/DPLRouteMatcherSpec.m @@ -186,4 +186,36 @@ }); }); + +describe(@"Matching on Schemes", ^{ + + __block NSURL *url1; + __block NSURL *url2; + beforeEach(^{ + url1 = [NSURL URLWithString:@"derp://dpl.io/say/hello"]; + url2 = [NSURL URLWithString:@"foo://dpl.io/say/hello"]; + }); + + it(@"allows any scheme if not specified in the route", ^{ + DPLRouteMatcher *matcher = [DPLRouteMatcher matcherWithRoute:@"/say/hello"]; + DPLDeepLink *deepLink = [matcher deepLinkWithURL:url1]; + expect(deepLink).toNot.beNil(); + + deepLink = [matcher deepLinkWithURL:url2]; + expect(deepLink).toNot.beNil(); + }); + + it(@"matches a url with a scheme specific route", ^{ + DPLRouteMatcher *matcher = [DPLRouteMatcher matcherWithRoute:@"derp://(.*)/say/hello"]; + DPLDeepLink *deepLink = [matcher deepLinkWithURL:url1]; + expect(deepLink).toNot.beNil(); + }); + + it(@"does NOT match a url with a different scheme than the route", ^{ + DPLRouteMatcher *matcher = [DPLRouteMatcher matcherWithRoute:@"derp://(.*)/say/hello"]; + DPLDeepLink *deepLink = [matcher deepLinkWithURL:url2]; + expect(deepLink).to.beNil(); + }); +}); + SpecEnd From c07425ebec5b650225dc8e82937a5323befc42fc Mon Sep 17 00:00:00 2001 From: Wes Smith Date: Wed, 13 May 2015 13:40:34 -0400 Subject: [PATCH 2/2] add scheme matching example to readme --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 09660dd..68a2d79 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,17 @@ router[@"timeline"] = ^{ … } ``` +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: +An incoming URL of `scheme-one://timeline` + +```objc +// Matches the URL. +router[@"scheme-one://timeline"] = ^{ … } + +// Does not match the URL. +router[@"scheme-two://timeline"] = ^{ … } +``` ## Running the Demo