- 06 Sep 2024
- 3 Minutes To Read
-
Print
-
DarkLight
-
PDF
How to set up a Deep link in iOS?
- Updated On 06 Sep 2024
- 3 Minutes To Read
-
Print
-
DarkLight
-
PDF
Deep link enables you to link directly to the content present in your app itself. You can embed a deep link in Whatfix Mobile projects like Walkthrough, Pop-ups, etc, and inside project elements like buttons, texts, etc. After the element is clicked, the added deep link page opens.
Set up Deep link
Add the URL Scheme to your project:
- Open your project.xcodeproj. Under targets, select your app and go to the info section.
- Scroll down to see the URL Types and click + to add a new URL Type.
- Add your app's name or any desired name in the URL Schemes field. This is your schemeName.
- Add your app's bundle identifier or any unique identifier in the identifier field.
This is not a mandatory step.
Below iOS 13
- Add the open URL method in the AppDelegate.swift file.
- In the Whatfix Mobile Dashboard, make sure to use the format schemeName://pageName.
Example: demoApp://profile - If the schemeName in your project URL Schemes matches with the Dashboard schemeName, the user is redirected to the following method.
If you have a profile page and you want to add deep link capability to that, use the following code snippet,
// AppDelegate.swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : An] = [:]) -> Bool {
guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
let host = components.host else {
return false
}
// Extract host. Host is your pageName. Based on the host name, go to the
// desired view or view controller like below or you can do in your way based
// on your app's architecture.
guard let deeplink = DeepLink(rawValue: host) else {
return
}
mainController?.handleDeeplink(deeplink)
return true
}
// global type
enum DeepLink: String {
case home
case profile
}
// MainController.swift
func handleDeeplink(_ deeplink: DeepLink) {
switch deeplink {
case .home:
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
self.navigationController?.pushViewController(vc!, animated: true)
case .profile:
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "ProfileViewController") as? ProfileViewController
self.navigationController?.pushViewController(vc!, animated: true)
}
}
iOS 13 and above
- Add the open URLContexts method in the SceneDelegate.swift file
- In the Whatfix Mobile Dashboard, make sure to use the format schemeName://pageName.
Ex: demoApp://profile - If the schemeName in your project URL Schemes matches with the Dashboard schemeName, the user is redirected to the following method.
If you have a profile page and you want to add deep link capability to that, use the following code snippet,
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else { return }
guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
let host = components.host else {
return
}
// Extract host. Host is your pageName. Based on the host name, go to the
// desired view or view controller like below or you can do in your way based
// on your app's architecture.
guard let deeplink = DeepLink(rawValue: host) else {
return
}
mainController?.handleDeeplink(deeplink)
}
// global type
enum DeepLink: String {
case home
case profile
}
// MainController.swift
func handleDeeplink(_ deeplink: DeepLink) {
switch deeplink {
case .home:
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
self.navigationController?.pushViewController(vc!, animated: true)
case .profile:
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "ProfileViewController") as? ProfileViewController
self.navigationController?.pushViewController(vc!, animated: true)
}
}
Deep links in Flow Menu
When configuring a Flow Menu (i.e. Walkthrough Checklist/Walkthrough Menu), for a particular menu, item you can add a deep link. It is better if a particular menu item of the Flow Menu is configured on a different page (not the same as the start screen). With the help of deep link, Whatfix Mobile can go to the required page.
Consider that you want to add a deep link in a Walkthrough Checklist,
The menu item Profile is configured on a different page (not the same as the start screen). To go to the required page, you can add the deep link using the following steps,
- Click Profile.
- On the right, under the DEEPLINK section, add your configured deep-link URL.
The following image shows adding a deep link in the Dashboard with the format, schemeName://pageName,