This code is only available for iOS 13 as a minimum target. I added a code example to add a realated share button in a SwiftUI view in case other people need it. This code also work for iPad.
You can use this class LinkMetadataManager
and add the image of your choice. The very important part, is that you must have your image in your project directory, not in the Assets.xcassets folder. Otherwise, it won't work.
When everything will be setup, you will use the button this way in your SwiftUI view.
struct ContentView: View {
var body: some View {
VStack {
ShareButton()
}
}
}
This is the class that will be sharing your application with the Apple Store link. You can share whatever you want from that. You can see how the image is added using LPLinkMetadata
, as it is the part that interests you.
import LinkPresentation
/// Transform url to metadata to populate to user.
final class LinkMetadataManager: NSObject, UIActivityItemSource {
var linkMetadata: LPLinkMetadata
let appTitle = "Your application name"
let appleStoreProductURL = "https://apps.apple.com/us/app/num8r/id1497392799" // The url of your app in Apple Store
let iconImage = "appIcon" // The name of the image file in your directory
let png = "png" // The extension of the image
init(linkMetadata: LPLinkMetadata = LPLinkMetadata()) {
self.linkMetadata = linkMetadata
}
}
// MARK: - LinkMetadataManager Setup
extension LinkMetadataManager {
/// Creating metadata to be populated in the share sheet.
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
guard let url = URL(string: appleStoreProductURL) else { return linkMetadata }
linkMetadata.originalURL = url
linkMetadata.url = linkMetadata.originalURL
linkMetadata.title = appTitle
linkMetadata.iconProvider = NSItemProvider(
contentsOf: Bundle.main.url(forResource: iconImage, withExtension: png))
return linkMetadata
}
/// Showing an empty string returns a share sheet with the minimum requirement.
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return String()
}
/// Sharing the application url.
func activityViewController(_ activityViewController: UIActivityViewController,
itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
return linkMetadata.url
}
}
Use this extension of View
to trigger the share sheet on a SwiftUI view.
import SwiftUI
// MARK: View+ShareSheet
extension View {
/// Populate Apple share sheet to enable the user to share Apple Store link.
func showAppShareSheet() {
guard let source = UIApplication.shared.windows.first?.rootViewController else {
return
}
let activityItemMetadata = LinkMetadataManager()
let activityVC = UIActivityViewController(
activityItems: [activityItemMetadata],
applicationActivities: nil)
if let popoverController = activityVC.popoverPresentationController {
popoverController.sourceView = source.view
popoverController.permittedArrowDirections = []
popoverController.sourceRect = CGRect(x: source.view.bounds.midX,
y: source.view.bounds.midY,
width: .zero, height: .zero)
}
source.present(activityVC, animated: true)
}
}
Then, create a ShareButton
as a component to use it in any of your SwiftUI view. This is what is used in the ContentView.
import SwiftUI
/// Share button to send app store link using the
/// Apple classic share screen for iPhone and iPad.
struct ShareButton: View {
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
var body: some View {
ZStack {
Button(action: { showAppShareSheet() }) {
Image(systemName: "square.and.arrow.up")
.font(horizontalSizeClass == .compact ? .title2 : .title)
.foregroundColor(.accentColor)
}
.padding()
}
}
}