6

How to Define Custom URL Actions for SwiftUI Text Views

 1 year ago
source link: https://swiftsenpai.com/development/swiftui-custom-url-action/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Define Custom URL Actions for SwiftUI Text Views

With the release of iOS 15, Apple has introduced native markdown support to the SwiftUI Text view. This feature allows developers to easily create strings with hyperlinks that can open websites, send emails, or make phone calls. While this feature may be sufficient in most cases, there are situations where developers might want more control over what happens when a link is tapped.

In this article, I will show you how to achieve greater control over link behavior in SwiftUI. Additionally, I will also share a few interesting use cases of custom URL action that you might find useful.

So, without further ado, let’s jump right in!


Using the `openURL` Environment View Modifier

With the release of iOS 15, Apple introduced the openURL environment view modifier, along with markdown support in the Text view. This modifier allows developers to define the action that will occur when a link is tapped. It can be easily used by appending it to a Text view, as shown in the sample code below:

Text("Find out more [here](https://www.apple.com)")
    .environment(\.openURL, OpenURLAction { url in
        
        // Do something here...
        
        return .systemAction
    })

It is important to note that at the end of the action handler, it is required to return the result of the action performed. This ensures that the system knows the final outcome of the custom action.

If you’re wondering about the available action results, here is a list of all the possibilities:

All available results of a custom open URL action in SwiftUI

Source: developer.apple.com

In the next section, we’ll take a look at some of the interesting use cases for this modifier.


Some Interesting Use Cases

Now that you understand how the openURL environment view modifier works, let’s take a look at a few of its interesting use cases.

Analytic Tracking

Let’s say we want to keep track of which URLs in a Text view users tap the most for analytical purposes, we can do it using the following approach:

Text("""
      Find out more:
        - [iPhone](https://www.apple.com/iphone/)
        - [iPad](https://www.apple.com/ipad/)
        - [Mac](https://www.apple.com/mac/)
""")
.environment(\.openURL, OpenURLAction { url in

    // Track url tapped
    Analytic.track(url)

    // Open the URL using default browser
    return .systemAction
})

After finishing tracking, it’s important not to modify the system’s default behavior. To achieve this, we can return .systemAction as the action result. This ensures that the system will proceed by opening the URL in the default browser.

Trigger Non-URL Related Actions

Besides normal URL interaction, we can also use the modifier to perform actions similar to a button. Please take a look at the following GIF:

Define custom URL action that is non-URL related
Changing color with links

Here’s how it’s done.

Text("Choose text color: [Red](FF0000), [Green](00FF00), [Blue](0000FF)")
    .environment(\.openURL, OpenURLAction { url in

        withAnimation {
            // Init color using hex
            // Refer: https://stackoverflow.com/a/56874327
            textColor = Color(hex: url.absoluteString)
        }

        return .handled
    })
    .foregroundColor(textColor)

As you can see, the markdown URL syntax is not limited to just URLs. We can essentially give it any string and pass it to the action handler. In this case, we are passing in the color hex code.

URL Modification

In cases where we need to modify the destination URL before opening it, the systemAction(_:) action result will come in handy. For instance, we can use it to append a UTM source to the destination URL.

Text("Find out more [here](https://www.apple.com)")
    .environment(\.openURL, OpenURLAction { url in

        // Create the URL with query items
        let queryItems = [URLQueryItem(name: "utm_source", value: "swiftsenpai")]
        let newURL = url.appending(queryItems: queryItems)

        // Open the URL with query items
        return .systemAction(newURL)
    })

Doing so will change the destination URL to:

https://www.apple.com/?utm_source=swiftsenpai

I hope you will find this article helpful. If you like this article, be sure to check out my other articles related to iOS development. You can also follow me on Twitter, and LinkedIn, and subscribe to my newsletter so that you won’t miss out on any of my upcoming articles.

Thanks for reading. 👨🏻‍💻


Recommend

  • 52

    Part 2 in a series on understanding data in SwiftUI. We will talk about the key that makes principles inpart 1 possible in SwiftUI. And how this resulting in a reduction of the complexity of UI development.

  • 49
    • www.ioscreator.com 4 years ago
    • Cache

    SwiftUI Overlay Views Tutorial

    With SwiftUI layers can be added on top of the view. This can be done with the .overlay modifier. In this tutorial a text view is displayed on top of an image using an overlay.SwiftUI requires Xcode 11 and MacOS Catalina...

  • 33
    • swiftwithmajid.com 4 years ago
    • Cache

    Combine and SwiftUI views

    Combine is one of the new frameworks released during WWDC 2019. It provides a declarative Swift API for processing values over time. Today we will talk about one of the hidden features of SwiftUI...

  • 18

    SwiftUI provides us a very fast and easy to use diffing algorithm, but as you might know, diffing is a linear operation. It means that diffing will be very fast for simple layouts and can take some time for a c...

  • 25
    • swiftwithmajid.com 4 years ago
    • Cache

    Using UIKit views in SwiftUI

    A few weeks ago, we talked about building views like PagerView and BottomSheetView from scratch in SwiftUI . SwiftUI is pretty young and misses some components that we expect to hav...

  • 28
    • swiftwithmajid.com 4 years ago
    • Cache

    Fitting and filling views in SwiftUI

    This week I want to continue the topic of layout system in SwiftUI . The SwiftUI layout engine works predictably, and usually, an outcoming result looks like we expect. Today, to make this process even...

  • 9
    • worthdoingbadly.com 3 years ago
    • Cache

    Rendering SwiftUI views to HTML

    I built a proof-of-concept tool to render SwiftUI to HTML. While I’m not intending to turn it into a full UI framework, I still learned plenty along the way: I learned how to use Swift’s generic...

  • 12
    • looseyi.github.io 3 years ago
    • Cache

    WWDC20 - Build SwiftUI views for widgets

    WWDC20 Session 10033 - Build SwiftUI views for widgets 本文知识目录

  • 6
    • www.ioscreator.com 3 years ago
    • Cache

    SwiftUI Custom Swipe Actions Tutorial

    SwiftUI Custom Swipe Actions Tutorialin SwiftUI a custom swipe action can be displayed when an row on a List is swiped. The action can be displayed left or right and can be displayed with a custom tint color and a system image.. I...

  • 10

    How to define custom environment values in SwiftUI 30 Aug 2021 ⋅ 4 min read ⋅ SwiftUI Table of Cont...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK