In iMessage when you link a twitter post you have the following preview
As you can see the content of the tweet is present.
I wanted to replicate that so I used LPLinkMetadata and even found the "hidden" metadata.value(forKey: "_summary")
to get the description. Below is the full code
import SwiftUI
import LinkPresentation
class LinkViewModel : ObservableObject {
let metadataProvider = LPMetadataProvider()
@Published var metadata: LPLinkMetadata?
@Published var image: UIImage?
init(link : String) {
guard let url = URL(string: link) else {
return
}
metadataProvider.startFetchingMetadata(for: url) { (metadata, error) in
guard error == nil else {
assertionFailure("Error")
return
}
DispatchQueue.main.async {
self.metadata = metadata
}
guard let imageProvider = metadata?.imageProvider else { return }
imageProvider.loadObject(ofClass: UIImage.self) { (image, error) in
guard error == nil else {
// handle error
return
}
if let image = image as? UIImage {
// do something with image
DispatchQueue.main.async {
self.image = image
}
} else {
print("no image available")
}
}
}
}
}
struct MetadataView : View {
@StateObject var vm : LinkViewModel
var body: some View {
VStack {
if let metadata = vm.metadata {
Text(metadata.title ?? "no title")
Text(metadata.value(forKey: "_summary") as? String ?? "np description" )
}
if let uiImage = vm.image {
Image(uiImage: uiImage)
.resizable()
.frame(width: 100, height: 100)
}
}
}
}
struct ContentView: View {
var links = [ "https://www.google.com", "https://www.hotmail.com", "https://twitter.com/t3dotgg/status/1764398959513276630"]
let metadataProvider = LPMetadataProvider()
var body: some View {
List(links, id:\.self) { item in
Section{
VStack {
Text(item)
// Image(systemName: "heart.fill")
MetadataView(vm: LinkViewModel(link: item))
}
}
}
}
}
With no luck, I even tried the third party Swift OpenGraph libraires (getting the og:title), I printed the html with no luck, added user-agent and stuff and still can't.
There was a great discussion on the mastodon github about it (for server side implementation) but replicated the iMessage behaviour seems hard.
Any tips ? :-)