Providing custom meta data to UIActivityViewController doesn't seem to work

what am I missing here?

import SwiftUI
import LinkPresentation
import CoreServices

struct ActivityView: UIViewControllerRepresentable {
  private let activityItems: [Any]
  private let applicationActivities: [UIActivity]?
  private let completion: UIActivityViewController.CompletionWithItemsHandler?
  @Binding var isPresented: Bool
  
  init(isPresented: Binding<Bool>,
       items: [Any], activities: [UIActivity]? = nil,
       onComplete: UIActivityViewController.CompletionWithItemsHandler? = nil) {
    _isPresented = isPresented
    activityItems = items
    applicationActivities = activities
    completion = onComplete
  }
  
  func makeUIViewController(context: Context) -> ActivityViewControllerWrapper {
    ActivityViewControllerWrapper(isPresented: $isPresented, activityItems: activityItems, applicationActivities: applicationActivities, onComplete: completion)
  }
  
  func updateUIViewController(_ uiViewController: ActivityViewControllerWrapper, context: Context) {
    uiViewController.isPresented = $isPresented
    uiViewController.completion = completion
    
    uiViewController.updateState()
  }
  
}

final class ActivityViewControllerWrapper: UIViewController {
  var activityItems: [Any]
  var applicationActivities: [UIActivity]?
  var isPresented: Binding<Bool>
  var completion: UIActivityViewController.CompletionWithItemsHandler?
  
  init(isPresented: Binding<Bool>,
       activityItems: [Any], applicationActivities: [UIActivity]? = nil,
       onComplete: UIActivityViewController.CompletionWithItemsHandler? = nil) {
    self.activityItems = activityItems
    self.applicationActivities = applicationActivities
    self.isPresented = isPresented
    
    super.init(nibName: nil, bundle: nil)
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  override func didMove(toParent parent: UIViewController?) {
    super.didMove(toParent: parent)
    updateState()
  }
  
  fileprivate func updateState() {
    let isActivityPresented = presentedViewController != nil
    
    if isActivityPresented != isPresented.wrappedValue {
      if !isActivityPresented {
        let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: applicationActivities)
        controller.popoverPresentationController?.sourceView = view
        controller.completionWithItemsHandler = { [weak self] (activityType, success, items, error) in
          self?.isPresented.wrappedValue = false
          self?.completion?(activityType, success, items, error)
        }
        present(controller, animated: true, completion: nil)
      }
    }
  }
}

extension ActivityViewControllerWrapper: UIActivityItemSource {
  func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
    return ""
  }
  
  func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
    return nil
  }
  
  func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
    let metadata = LPLinkMetadata()
    
    // ignore the forces, please
    metadata.originalURL = URL(string: self.activityItems.first! as! String)!
    metadata.url = metadata.originalURL
    metadata.title = "Snapshot"
    metadata.imageProvider = NSItemProvider.init(object: xxxx)
    return metadata
  }
}

This topic was automatically closed after 166 days. New replies are no longer allowed.