Deep Linking
Contents
Deep linking is a powerful technique that, with some preparation by a developer, can lead to a very advanced form of messaging. Deep linking is a process by where a URL is opened by your app, and then in response your app is opened to a specific screen. You might be familiar with deep linking on Facebook, where URIs are in the form of fb://something
. In deep links, the protocol is the name of your app (or similar) and your app defines the routes the URL maps to.
iOS
On iOS deep linking occurs with Custom URL Schemes. There are 3 main parts to this.- Define your URL Scheme in you app's Info.plist
- Implement
-application:openURL:options:
in your app delegate - Inside this, parse the URL for any parameters, initialize the correct view controllers and present them.
myapp://?screen=new_post
.
Objective-C
//In your App Delegate
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(nonnull NSDictionary *)options {
NSDictionary *params = [self queryParamsFromURL:url];
if ([[params objectForKey:@"screens"] isEqualToString:@"new_post"]) {
NewPostViewController *npViewController = [NewPostViewController alloc] init];
[self.window.rootViewController presentViewController:npViewController];
} // else if other supported views, keys and values here
return YES;
}
- (NSDictionary *)queryParamsFromURL:(NSURL *)url {
NSArray *queryComponents = [[url query] componentsSeparatedByString:@"&"];
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:[queryComponents count]];
for (NSString *queryComponent in queryComponents) {
NSArray *paramComponents = [queryComponent componentsSeparatedByString:@"="];
id object = nil;
if ([paramComponents count] > 1) {
object = [paramComponents[1] stringByRemovingPercentEncoding];
}
else {
object = [NSNull null];
}
NSString *key = [paramComponents[0] stringByRemovingPercentEncoding];
[parameters setObject:object forKey:key];
}
return parameters;
}
Swift
//In your App Delegate
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let params = self.queryParams(url: url)
let screens = params["screens"]
if screens == "new_post" {
let npViewController = NewPostViewController()
self.window?.rootViewController?.present(npViewController, animated: true, completion: nil)
}// else if other supported views, keys and values here
return true
}
func queryParams(url: URL) -> Dictionary<String, String> {
var parameters = Dictionary<String, String>()
if let queryComponents = url.query?.components(separatedBy: "&") {
for queryComponent in queryComponents {
let paramComponents = queryComponent.components(separatedBy: "=")
var object : String? = nil
if paramComponents.count > 1 {
object = paramComponents[1]
}
let key = paramComponents[0]
parameters[key] = object
}
}
return parameters
}
- If the deep link comes from the Sailthru Mobile Message Stream, you may want to dismiss the message stream before navigating to your desired view.
- iOS 9 Requires an additional key to previously, where you need to define an array of schemes using the LSApplicationQueriesSchemes key.
Android
On Android, deep linking occurs with the use of Intent Filters. There are 3 main parts to this. First, define your Intent Filter in your app's manifest file, with the<data>
element filled out. This must be associated with an activity, typically the root.
Then, read the Intent
that was used to start your application in the onCreate
method of your activity. If your application is already running, the Intent will be sent to the onNewIntent(Intent)
method of the receiving Activity.
Java
Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
String valueOne = uri.getQueryParameter("keyOne");
String valueTwo = uri.getQueryParameter("keyTwo");
}
Kotlin
val intent = intent
if (Intent.ACTION_VIEW == intent.action) {
val uri: Uri = intent.data
val valueOne: String = uri.getQueryParameter("keyOne")
val valueTwo: String = uri.getQueryParameter("keyTwo")
}