button / DeepLinkKit

A splendid route-matching, block-based way to handle your deep links.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deep Links to Routes with a Single Path Component Break with Trailing Slash

mliberatore opened this issue · comments

Using 1.2.1, I’m noticing an issue with routes that consist of a single path component failing to be handled when a trailing slash is included in the URL. This issue is not present when there is more than one path component in the registered route.

Example

In Info.plist, I have the following scheme, test, set up so that my test app opens from Mobile Safari for URLs that begin with test://.

screen shot 2016-11-08 at 4 16 51 pm

This is the entirety of AppDelegate.m in an otherwise empty test project, in which I demonstrate the issue:

#import "AppDelegate.h"
#import <DeepLinkKit/DeepLinkKit.h>

@interface AppDelegate ()

@property (nonatomic) DPLDeepLinkRouter *router;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.router = [[DPLDeepLinkRouter alloc] init];
    
    self.router[@"search"] = ^(DPLDeepLink *link) {
        // Called for `test://search`
        // Not called for `test://search/` <-- THE ISSUE
        NSLog(@"Handled Search Link");
    };
    
    self.router[@"settings/about"] = ^(DPLDeepLink *link) {
        // Called for `test://settings/about`
        // Called for `test://settings/about/`
        NSLog(@"Handled settings/about Link");
    };
    
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [self.router handleURL:url withCompletion:NULL];
}

@end

In summary, the issue shown in this example is that when attempting to open test://search/ in Mobile Safari the handler that logs the message "Handled Search Link" is not called.

Workaround

There is a workaround to the issue, and that is to specify /? to the end of the registered route to allow for an optional trailing slash. In the example above, that’d change the registration of search to be:

self.router[@"search/?"] = ^(DPLDeepLink *link) {
    // Called for `test://search`
    // Called for `test://search/`
    NSLog(@"Handled Search Link");
};

However, this is not ideal as it requires treating routes with different numbers of components differently.