Created
October 29, 2019 18:57
-
-
Save dbolella/3db504fab5405b4afcfc5ea63014aab8 to your computer and use it in GitHub Desktop.
Working LinkPreview in SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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