如何在swift中将图标添加到共享表?

如何在swift中将图标添加到共享表?,swift,xcode,ios-sharesheet,Swift,Xcode,Ios Sharesheet,我正在iOS应用程序中使用共享表。我想知道如何在它打开时将图标添加到它的左上角。我加了一张照片来说明我的意思 [我的意思示例照片][1] 为了在UIActivityViewController中获取图像,我尝试了上面的代码示例。每当我们尝试共享消息以及url和图像时,图像都不会出现。如果我们只向activityItems添加url,则会显示url中的图像 我检查了两个有共享选项的应用程序,如果在共享应用程序时有文本,则不会显示图像。如果只有url,则显示url中的图像 图标是从您添加到内容中的

我正在iOS应用程序中使用共享表。我想知道如何在它打开时将图标添加到它的左上角。我加了一张照片来说明我的意思

[我的意思示例照片][1]


为了在
UIActivityViewController
中获取图像,我尝试了上面的代码示例。每当我们尝试共享
消息
以及
url
图像
时,图像都不会出现。如果我们只向activityItems添加
url
,则会显示url中的图像

我检查了两个有共享选项的应用程序,如果在共享应用程序时有文本,则不会显示图像。如果只有url,则显示url中的图像


图标是从您添加到内容中的链接获取的,因此,如果您要添加任何应用程序的链接,它将自动从该链接捕获图标,您将在那里看到图标,但是,正如您所提到的,如果您将任何消息和图像添加到一起,或者将任何消息和链接添加到一起,那么它将只显示您在objectToShare数组中附加的第一项。此代码仅作为最低目标用于iOS 13。我添加了一个代码示例,以便在SwiftUI视图中使用共享按钮,以防其他人需要它。这段代码也适用于iPad

您可以使用此类
LinkMetadataManager
并添加您选择的图像。非常重要的一点是,必须将图像放在项目目录中,而不是放在Assets.xcsets文件夹中。否则,它将不起作用

当一切都设置好后,您将在SwiftUI视图中以这种方式使用按钮

struct ContentView: View {
    
  var body: some View {
    VStack {
      ShareButton()
    }
  }
}
import SwiftUI

//  MARK: View+ShareSheet
extension View {

  /// Populate Apple share sheet to enable 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)
  }
}
这是将与Apple Store链接共享您的应用程序的类。你可以分享你想要的任何东西。您可以使用
LPLinkMetadata
查看图像是如何添加的,因为它是您感兴趣的部分

import LinkPresentation

//  MARK: LinkMetadataManager
/// 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: - Link Meta Manager Setup
extension LinkMetadataManager {
  /// Creating metadata to population 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 empty string returns a share sheet with the minimum requirement.
  ///
  func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
    return String()
  }

  /// Sharing url of the application.
  ///
  func activityViewController(_ activityViewController: UIActivityViewController,
                              itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
    return linkMetadata.url
  }
}
使用
视图的此扩展
在SwiftUI视图上触发共享表

struct ContentView: View {
    
  var body: some View {
    VStack {
      ShareButton()
    }
  }
}
import SwiftUI

//  MARK: View+ShareSheet
extension View {

  /// Populate Apple share sheet to enable 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

//  MARK: ShareButton
/// 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()
    }
  }
}

这是迄今为止我遇到的最好的解决方案!LPLinkMetadata的巧妙使用