Created
January 7, 2020 00:56
-
-
Save leamars/cd27fce7589ae6389847fd5375775f8d to your computer and use it in GitHub Desktop.
LinkPresentation SwiftUI
This file contains hidden or 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
import SwiftUI | |
import LinkPresentation | |
struct TutorialPost: UIViewRepresentable { | |
typealias UIViewType = LPLinkView | |
var url: URL | |
func makeUIView(context: UIViewRepresentableContext<TutorialPost>) -> TutorialPost.UIViewType { | |
return LPLinkView(url: url) | |
} | |
func updateUIView(_ uiView: TutorialPost.UIViewType, context: UIViewRepresentableContext<TutorialPost>) { | |
if let cachedData = MetaCache.retrieve(urlString: url.absoluteString) { | |
uiView.metadata = cachedData | |
uiView.sizeToFit() | |
} else { | |
let provider = LPMetadataProvider() | |
provider.startFetchingMetadata(for: url) { metadata, error in | |
guard let metadata = metadata, error == nil else { | |
return | |
} | |
MetaCache.cache(metadata: metadata) | |
DispatchQueue.main.async { | |
uiView.metadata = metadata | |
uiView.sizeToFit() | |
} | |
} | |
} | |
} | |
} | |
struct ContentView: View { | |
var links: [String] = [ | |
"https://www.raywenderlich.com/5429927-beginning-collection-views", | |
"https://www.raywenderlich.com/6849561-layout-in-ios", | |
"https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types", | |
"https://www.raywenderlich.com/6484760-create-a-splash-screen-with-swiftui", | |
"https://www.raywenderlich.com/6275408-create-a-drawing-app-with-pencilkit", | |
"https://www.raywenderlich.com/6177504-continuous-integration", | |
"https://www.raywenderlich.com/5429927-beginning-collection-views", | |
"https://www.raywenderlich.com/6849561-layout-in-ios", | |
"https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types", | |
"https://www.raywenderlich.com/6484760-create-a-splash-screen-with-swiftui", | |
"https://www.raywenderlich.com/6275408-create-a-drawing-app-with-pencilkit", | |
"https://www.raywenderlich.com/6177504-continuous-integration", | |
] | |
var body: some View { | |
// Issues with reusability | |
// List { | |
// ForEach(links, id: \.self) { link in | |
// TutorialPost(url: URL(string: link)!) | |
// } | |
// } | |
ScrollView { | |
ForEach(links, id: \.self) { link in | |
TutorialPost(url: URL(string: link)!) | |
} | |
} | |
} | |
} | |
struct MetaCache { | |
static func cache(metadata: LPLinkMetadata) { | |
do { | |
let data = try NSKeyedArchiver.archivedData(withRootObject: metadata, requiringSecureCoding: true) | |
UserDefaults.standard.setValue(data, forKey: metadata.url!.absoluteString) | |
} | |
catch let error { | |
print("Error when cachine: \(error.localizedDescription)") | |
} | |
} | |
static func retrieve(urlString: String) -> LPLinkMetadata? { | |
do { | |
guard let data = UserDefaults.standard.object(forKey: urlString) as? Data, | |
let metadata = try NSKeyedUnarchiver.unarchivedObject(ofClass: LPLinkMetadata.self, from: data) else { return nil } | |
return metadata | |
} | |
catch let error { | |
print("Error when cachine: \(error.localizedDescription)") | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have any working example with UITableView ? raywenderlich article suggest to use stackview instead of tableview.