19

I have a simple widget (medium-sized) with two texts, and what I want is to be able to perform a deep link to lead the user to a specific section of my app, but I can't seem to find a way to do so.

The view I have written (which is very simple):

HStack {
    Text("FIRST ITEM")    
    Spacer()
    Text("SECOND ITEM")
}

I have already tried to replace

Text("SECOND ITEM")

with

Link("SECOND ITEM destination: URL(string: myDeeplinkUrl)!)

but it doesn't work either.

3 Answers 3

46
  1. In the Widget view you need to create a Link and set its destination url:
struct SimpleWidgetEntryView: View {
    var entry: SimpleProvider.Entry

    var body: some View {
        Link(destination: URL(string: "widget://link1")!) {
            Text("Link 1")
        }
    }
}

Note that Link works in medium and large Widgets only. If you use a small Widget you need to use:

.widgetURL(URL(string: "widget://link0")!)
  1. In your App view receive the url using onOpenURL:
@main
struct WidgetTestApp: App {
    var body: some Scene {
        WindowGroup {
            Text("Test")
                .onOpenURL { url in
                    print("Received deep link: \(url)")
                }
        }
    }
}

It is also possible to receive deep links in the SceneDelegate by overriding:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>)

You can find more explanation on how to use this function in this thread:


Here is a GitHub repository with different Widget examples including the DeepLink Widget.

6
  • How is the link for .widgetUrl specified? How to define this url for a particular location in an app? Correct me if I am wrong but I think the first part of the url shows the URL scheme specified in the info section of the app but I am not able to figure out what exactly the second part of the url is?
    – Krits
    Dec 9, 2020 at 12:07
  • @Krits Every url from the widget will be opened in the app. So the scheme can be a way to differentiate widget urls from other urls (e.g. from user notifications). The other path components can specify an action/location - it's up to you. You just need to use onOpenURL(url:) in a specific view and check if a particular URL arrived. Dec 9, 2020 at 13:56
  • So as mentioned in the above question, "widget://link0". What exactly link0 and link1 are? And how do we specify the action and or location in this url?
    – Krits
    Dec 9, 2020 at 19:24
  • @Krits It's just a custom text describing the action. In onOpenURL(url:) you just check which url arrived (if it's link0 or link1...) and perform some action (show an alert, activate a navigation link...). Dec 9, 2020 at 22:02
  • 1
    beautiful and complete answer
    – Mona
    Nov 14, 2021 at 4:55
11

Also, you can do it using AppDelegate (if you not using SceneDelegate):

.widgetURL(URL(string: "urlsceheme://foobarmessage"))

// OR

Link(destination: URL(string: "urlsceheme://foobarmessage")!) {
    Text("Foo")
}

Set this code within AppDelegate

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
   let message = url.host?.removingPercentEncoding // foobarmessage
   return true
}
1
  • using above code i am not getting any message in AppDelegate any suggestion to cross check setup ? Jun 21, 2021 at 5:32
2

See docs on: Respond to User Interactions

When users interact with your widget, the system launches your app to handle the request. When the system activates your app, navigate to the details that correspond to the widget’s content. Your widget can specify a URL to inform the app what content to display. To configure custom URLs in your widget:

  • For all widgets, add the widgetURL(_:) view modifier to a view in your widget’s view hierarchy. If the widget’s view hierarchy includes more than one widgetURL modifier, the behavior is undefined.
  • For widgets that use WidgetFamily.systemMedium or WidgetFamily.systemLarge, add one or more Link controls to your widget’s view hierarchy. You can use both widgetURL and Link controls. If the interaction targets a Link control, the system uses the URL in that control. For interactions anywhere else in the widget, the system uses the URL specified in the widgetURL view modifier.

For example, a widget that displays details of a single character in a game can use widgetURL to open the app to that character’s detail.

@ViewBuilder
var body: some View {
    ZStack {
        AvatarView(entry.character)
            .widgetURL(entry.character.url)
            .foregroundColor(.white)
    }
    .background(Color.gameBackground)
}

If the widget displays a list of characters, each item in the list can be in a Link control. Each Link control specifies the URL for the specific character it displays.

When the widget receives an interaction, the system activates the containing app and passes the URL to onOpenURL(perform:), application(_:open:options:), or application(_:open:), depending on the life cycle your app uses.

If the widget doesn’t use widgetURL or Link controls, the system activates the containing app and passes an NSUserActivity to onContinueUserActivity(_:perform:), application(_:continue:restorationHandler:), or application(_:continue:restorationHandler:). The user activity’s userInfo dictionary contains details about the widget the user interacted with. Use the keys in WidgetCenter.UserInfoKey to access these values from Swift code. To access the userInfo values from Objective-C, use the keys WGWidgetUserInfoKeyKind and WGWidgetUserInfoKeyFamily instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.