Created
February 4, 2020 04:45
-
-
Save ryanisnhp/47098294779a5eaaab87f48509a88b11 to your computer and use it in GitHub Desktop.
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 UIKit | |
import LinkPresentation | |
class ViewController: UIViewController { | |
@IBOutlet weak var sharingButton: UIButton! | |
private var metaData: LPLinkMetadata = LPLinkMetadata() { | |
didSet { | |
DispatchQueue.main.async { | |
self.shareURLWithMetadata(metaData: self.metaData) | |
} | |
} | |
} | |
var isFetchMetadataDynamic = true | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
func shareURLWithMetadata(metaData: LPLinkMetadata) { | |
let metadataItemSource = LinkPresentationItemSource(metaData: metaData) | |
let activity = UIActivityViewController(activityItems: [metadataItemSource], applicationActivities: []) | |
present(activity, animated: true) | |
} | |
@available(iOS 13.0, *) | |
func fetchURLPreview(url: URL) { | |
let metadataProvider = LPMetadataProvider() | |
metadataProvider.startFetchingMetadata(for: url) { (metadata, error) in | |
guard let data = metadata, error == nil else { | |
return | |
} | |
self.metaData = data | |
} | |
} | |
func getMetadataForSharingManually(title: String, url: URL, fileName: String, fileType: String) -> LPLinkMetadata { | |
let linkMetaData = LPLinkMetadata() | |
let path = Bundle.main.path(forResource: fileName, ofType: fileType) | |
linkMetaData.iconProvider = NSItemProvider(contentsOf: URL(fileURLWithPath: path ?? "")) | |
linkMetaData.originalURL = url | |
linkMetaData.title = title | |
return linkMetaData | |
} | |
@IBAction func onSharing(_ sender: UIButton) { | |
let url = URL(string: "https://www.apple.com/airpods-pro/")! | |
if isFetchMetadataDynamic { | |
//Fetch meta data from URL | |
fetchURLPreview(url: url) | |
} else { | |
//Add metadata manually | |
self.metaData = getMetadataForSharingManually(title: "[Custom Title] Airpods Pro", url: url, fileName: "airpods_pro_custom", fileType: "jpg") | |
shareURLWithMetadata(metaData: self.metaData) | |
} | |
} | |
} | |
class LinkPresentationItemSource: NSObject, UIActivityItemSource { | |
var linkMetaData = LPLinkMetadata() | |
//Prepare data to share | |
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? { | |
return linkMetaData | |
} | |
//Placeholder for real data, we don't care in this example so just return a simple string | |
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any { | |
return "Placeholder" | |
} | |
/// Return the data will be shared | |
/// - Parameters: | |
/// - activityType: Ex: mail, message, airdrop, etc.. | |
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? { | |
return linkMetaData.originalURL | |
} | |
init(metaData: LPLinkMetadata) { | |
self.linkMetaData = metaData | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment