Skip to content

Instantly share code, notes, and snippets.

@dbolella
Created October 29, 2019 18:57
Show Gist options
  • Save dbolella/3db504fab5405b4afcfc5ea63014aab8 to your computer and use it in GitHub Desktop.
Save dbolella/3db504fab5405b4afcfc5ea63014aab8 to your computer and use it in GitHub Desktop.
Working LinkPreview in SwiftUI
struct ContentView: View {
@State var togglePreview = false
var body: some View {
VStack {
URLPreview(previewURL: URL(string: "https://medium.com")!, togglePreview: $togglePreview)
.aspectRatio(contentMode: .fit)
.padding()
}
}
}
struct URLPreview : UIViewRepresentable {
var previewURL:URL
//Add binding
@Binding var togglePreview: Bool
func makeUIView(context: Context) -> LPLinkView {
let view = LPLinkView(url: previewURL)
let provider = LPMetadataProvider()
provider.startFetchingMetadata(for: previewURL) { (metadata, error) in
if let md = metadata {
DispatchQueue.main.async {
view.metadata = md
view.sizeToFit()
self.togglePreview.toggle()
}
}
}
return view
}
func updateUIView(_ uiView: LPLinkView, context: UIViewRepresentableContext<URLPreview>) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment