Last active
January 18, 2020 00:50
-
-
Save ycui1/34b8a0927976bf3cf1361a7c3fb506f8 to your computer and use it in GitHub Desktop.
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
class ArticleFetcher: ObservableObject { | |
@Published var article: Article | |
init(article: Article){ | |
self.article = article | |
getArticleWithURL(article.id) | |
} | |
func getArticleWithURL(_ url: String) { | |
let linkPreview = SwiftLinkPreview() | |
linkPreview.preview(url, onSuccess: { (response) in | |
self.article.title = response.title ?? "Article Title" | |
self.article.link = response.canonicalUrl | |
if let imageURL = response.image { | |
self.article.imageLink = response.image | |
guard let url = URL(string: imageURL) else { return } | |
URLSession.shared.dataTask(with: url) { (data, response, error) in | |
DispatchQueue.main.async { | |
self.article.imageData = data | |
} | |
}.resume() | |
} | |
}) { (error) in | |
print(error) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment