SwiftUI: How to Create an Asynchronous Link Preview

Tan Yun Ching
3 min readNov 3, 2023

Create a Simple Asynchronous SwiftUI Link Preview

The idea of building this asynchronous link preview is similar to AsyncImage. It is to show a view that asynchronously loads and displays a link preview. Instead of showing nothing when we are trying to trigger the asynchronous call (startFetchingMetadata) to fetch the metadata (LPMetadataProvider) from the URL, we are showing a loading view when the call is trigger or an error view when there are error fetching the metadata.

Here is the example

Recording by author

Error View

I break it down into 2 main sections:

  1. Creating a SwiftUI LPLinkView: In this section, our focus will be on wrapping the LPLinkView within a SwiftUI view by conforming to the UIViewRepresentable protocol.
  2. Completing of Asynchronous Link Preview: This part addresses the different stages of the view presentation, which are contingent upon the status of the asynchronous call.

Creating a SwiftUI LPLinkView

struct LinkView: UIViewRepresentable {
var metadata: LPLinkMetadata

func makeUIView(context: Context) -> some UIView {
let linkView = LPLinkView(metadata: metadata)…

--

--