Apr 20, 2024 iOS

Perform a deeplink from SwiftUI widget on tap

Performing a deep link from a SwiftUI widget on tap involves several steps. Widgets in SwiftUI are limited in their interactivity compared to full app views, but you can still use them to open your app and navigate to a specific location using deep linking. Here’s a general approach:

  1. Set up Deep Linking in Your App: Configure your app to handle deep links. This involves setting up URL schemes or Universal Links in your app’s Info.plist and handling incoming URLs in your app delegate.
  2. Define the Deep Link URL: Decide on the URL format for your deep links. For example, myapp://details?id=123.
  3. Create a Widget with Tap Gesture: Define your widget and add a tap gesture recognizer to it. When the user taps the widget, it will trigger an action.
  4. Open Deep Link on Tap: In the action triggered by the tap gesture, open the deep link URL using UIApplication.shared.open().

Here’s an example implementation:

import SwiftUI

struct MyWidget: Widget {
    var body: some WidgetConfiguration {
        StaticConfiguration(kind: "MyWidget") { _ in
        .configurationDisplayName("My Widget")

struct WidgetView: View {
    var body: some View {
        VStack {
            Text("Tap to open deep link")
                .onTapGesture {
                    // Open the deep link URL when tapped
                    if let url = URL(string: "myapp://details?id=123"),
                       UIApplication.shared.canOpenURL(url) {

In this example:

  • We define a widget using Widget and WidgetConfiguration.
  • In the WidgetView, we display some content and attach an onTapGesture modifier to the text.
  • When the user taps the widget, it attempts to open the deep link URL using UIApplication.shared.open().
  • Replace "myapp://details?id=123" with the actual deep link URL of your app.

Make sure you handle the incoming deep link URL in your app delegate and navigate to the appropriate view based on the URL parameters.

Certainly! To perform a deep link from a SwiftUI widget on tap, you can follow these steps:

  1. Create the Widget View: In your widget view, create a Link and set its destination URL. Here’s an example:
struct SimpleWidgetEntryView: View {
    var entry: SimpleProvider.Entry

    var body: some View {
        Link(destination: URL(string: "widget://link1")!) {
            Text("Link 1")
  1. Note that Link works in medium and large widgets only. If you’re using a small widget, you need to use .widgetURL(URL(string: "widget://link0")!).
  2. Handle the Deep Link in Your App View: In your main app view, receive the URL using the onOpenURL modifier. For example
struct WidgetTestApp: App {
    var body: some Scene {
        WindowGroup {
                .onOpenURL { url in
                    print("Received deep link: \(url)")
                    // Handle the deep link here (e.g., navigate to a specific section).
  1. SceneDelegate (Optional): If you’re not using SceneDelegate, you can handle deep links in the AppDelegate by overriding the application(_:open:options:) method. Extract the URL from the options and handle it accordingly.

Remember that the URL scheme (widget://) can help differentiate widget URLs from other URLs. The other path components can specify actions or locations within your app. You can customize these URLs based on your app’s requirements 123.