Last active
January 6, 2020 23:02
-
-
Save leamars/ed699f8610f0a8c40673c504b4184f82 to your computer and use it in GitHub Desktop.
LinkPresentation
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 Foundation | |
import UIKit | |
import LinkPresentation | |
class ViewController: UIViewController { | |
let metadataProvider = LPMetadataProvider() | |
let url = URL(string: "https://www.raywenderlich.com/6613761-sharing-in-android-10-getting-started")! | |
var links: [String] = [] | |
var metadata: [LPLinkMetadata] = [] | |
@IBOutlet weak var linkView: UIView! | |
@IBOutlet weak var tableView: UITableView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
metadataProvider.startFetchingMetadata(for: url) { metadata, error in | |
guard error == nil else { | |
print("Error...") | |
return | |
} | |
// TODO: Make use of fetched data | |
self.metadata.append(metadata!) | |
// Present LPLinkMetadata in a LPLinkView | |
DispatchQueue.main.async { | |
let newLinkView = LPLinkView(metadata: metadata!) | |
newLinkView.translatesAutoresizingMaskIntoConstraints = false | |
// Below line doesn't seem to have any effect, even though there's a set size to the linkView in the storyboard | |
// newLinkView.sizeToFit() | |
self.linkView.addSubview(newLinkView) | |
// Doing this for testing purposes, so that we can ensure the metadata is | |
// all present by the time it hits the table view | |
self.tableView.reloadData() | |
} | |
} | |
} | |
} | |
extension ViewController: UITableViewDelegate, UITableViewDataSource { | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
currentMetadata = metadata[indexPath.row] | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return 1 | |
} | |
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
// Adding this height actually adds another view on top of the cell that covers the link | |
// that you can see in the view inspector | |
return 223 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "LinkCell", for: indexPath) | |
guard !metadata.isEmpty else { return UITableViewCell() } | |
let view = LPLinkView(metadata: metadata[0]) | |
//view.translatesAutoresizingMaskIntoConstraints = false | |
cell.contentView.addSubview(view) | |
view.sizeToFit() | |
// How it *should* work | |
// let provider = LPMetadataProvider() | |
// provider.startFetchingMetadata(for: url) { metadata, error in | |
// let view = LPLinkView(metadata: metadata!) | |
// cell.contentView.addSubview(view) | |
// view.sizeToFit() | |
// } | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment