In iMessage you can link a twitter post and it gets the image (if any), tweet content and title. Yet as a regular dev, I think, that we cannot get the tweet content like iMessage.
From this Github issue of Mastodon, I know that in the header there is everything we need, yet in a simple swift code using LPLinkMetadata we cannot get the description. Here is the code below
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)
MetadataView(vm: LinkViewModel(link: item))
}
}
}
}
}
The twitter link doesn't return any description, I also tried third party OG libraries with the og:title in Swift with no success, yet it works on iMessage.
Any tips ? :-)