此代码仅适用于 iOS 13 作为最低目标。我添加了一个代码示例以在 SwiftUI 视图中添加一个真实的共享按钮,以防其他人需要它。此代码也适用于 iPad。
您可以使用此类LinkMetadataManager
并添加您选择的图像。非常重要的部分是,您的图像必须在项目目录中,而不是在 Assets.xcassets 文件夹中。否则,它将无法正常工作。
当一切都设置好后,您将在 SwiftUI 视图中以这种方式使用按钮。
struct ContentView: View {
var body: some View {
VStack {
ShareButton()
}
}
}
这是将与 Apple Store 链接共享您的应用程序的类。你可以分享任何你想要的东西。您可以使用 来查看图像是如何添加的LPLinkMetadata
,因为它是您感兴趣的部分。
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
}
}
使用此扩展View
来触发 SwiftUI 视图上的共享表。
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)
}
}
然后,创建一个ShareButton
组件以在您的任何 SwiftUI 视图中使用它。这就是 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()
}
}
}