Firebase Dynamic Links: Getting Started

Learn how to use Firebase Dynamic Links to implement deep linking on iOS. By Danijela Vrzan.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 5 of 5 of this article. Click here to view the first page.

Handling Navigation in Your App

Open HomeView.swift. Below // Define environment property add:

@Environment(\.deepLink) var deepLink

To read the environment value from inside your HomeView() or any of its children, you define the environment property wrapper.

Next, add the following below // Define navigation:

// 1
.onChange(of: deepLink) { deepLink in
  guard let deepLink = deepLink else { return }

  switch deepLink {
  case .details(let recipeID):
    // 2
    if let index = recipes.firstIndex(where: {
      $0.recipeID == recipeID
    }) {
      // 3
      proxy.scrollTo(index, anchor: .bottom)
      // 4
      cellSelected = index
    }
  case .home:
    break
  }
}

Here's a code breakdown:

  1. You add onChange(of:perform:) to the view. It triggers an action every time a deepLink environment value changes.
  2. Next, you find the index of a recipe with the specified recipeID.
  3. ScrollViewReader is a view that provides programmatic scrolling by working with a proxy. You use the proxy's scrollTo(_:anchor:) to perform scrolling. It scans the scroll view until it finds the first child view with the specified index.
  4. Finally, you assign the index value to cellSelected. When it receives a new index value it automatically triggers navigation to the specific detailed view with the help of a NavigationLink.

Build and run. Select a recipe, tap Share and, using both Simulator and Terminal, open your dynamic link.

Now your app can lead the user to a specific recipe detailed view:

Simulator and Terminal apps side by side showing how to open dynamic link by pasting a command inside the terminal window and triggering the navigation to a specified recipe detailed view

Where to Go From Here?

Congratulations! Now that you know how to work with dynamic links, you can build on what you've learned in this tutorial by adding custom dynamic links to your apps.

You can download the completed version of the project using the Download Materials button at the top or bottom of this tutorial.

You can make the links you share richer and friendlier by using the LinkPresentation framework. If you'd like to learn how, check out this Visually Rich Links Tutorial for iOS: Image Thumbnails.

Be sure to check out Firebase Tutorial: Getting Started to learn how Firebase can supercharge your app. If you don't know where to begin, the Firebase Official Documentation is a good starting point.

I hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below.