6

UIActivityViewController dismisses the presenting view controller after sharing files. this is happening in iOS 13+ only. Is there any permanent solution for this? Others apps seem to have this issue too after updating to iOS 13.

   class VC : UIViewController {


   @IBAction func moveFiles(_ sender: UIButton) {

      let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)


    alertController.addAction(UIAlertAction(title: "Move", style: .default, handler: { action in

    let activityController = UIActivityViewController(activityItems: urls, applicationActivities: nil)


   if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
   activityController.popoverPresentationController?.sourceRect = sender.frame
    activityController.popoverPresentationController?.sourceView = sender.superview
    }
   self.present(activityController, animated: true, completion: nil)


    }))




           }


     }
Mariela
  • 139
  • 1
  • 7

4 Answers4

3

Here is the work around for your issue.

let tempController = TransparentViewController()
tempController.modalPresentationStyle = .overFullScreen

activityViewController.completionWithItemsHandler = { [weak tempController] _, _, _, _ in
  if let presentingViewController = tempController?.presentingViewController {
    presentingViewController.dismiss(animated: false, completion: nil)
  } else {
    tempController?.dismiss(animated: false, completion: nil)
  }
}

present(tempController, animated: true) { [weak tempController] in
    tempController?.present(activityViewController, animated: true, completion: nil)
}
Amrit Trivedi
  • 1,240
  • 1
  • 9
  • 24
0

Found similar question with solution which helps me. For iOS 13 show UIActivityViewController in another UIWindow Stackoverflow answer

Rustam Khisamov
  • 151
  • 1
  • 4
  • 8
0

Seems it's fixed in iOS 14.4

For older iOS versions I found easier workaround. Override dismiss(animated:completion:) with empty implementation so it won't be dismissing itself automatically. However, you can still dismiss this VC using super.dismiss(animated:completion).

E.g

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    // do nothing to workaround bug - automatically dimisses this VC after saveToCameraRoll activity was performed
    // call super.dismiss(animated:completion:) in order to really dismiss this VC
    // seems fixed in iOS 14.4
}

...

@objc private func didTapCloseButton(_ sender: UIButton) {
    super.dismiss(animated: true) // calling parent class implementation
}
Palli
  • 510
  • 6
  • 14
0

I have same issue right now on iOS target 14.1. I made my solution based on answers I found.

final class ShareViewController: UIViewController {

private let activityItems: [Any]
private let applicationActivities: [UIActivity]?

// Same looking initializer as UIActivityViewController has
init(activityItems: [Any], applicationActivities: [UIActivity]? = nil) {
    self.activityItems = activityItems
    self.applicationActivities = applicationActivities
    super.init(nibName: nil, bundle: nil)
    // Make transparent and covering entire screen
    view.backgroundColor = .clear
    modalPresentationStyle = .overCurrentContext
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // Present UIActivityViewController here
    presentShareSheet()
}


fileprivate func presentShareSheet() {
    let shareSheet = UIActivityViewController(activityItems: activityItems,
                                              applicationActivities: applicationActivities)
    shareSheet.completionWithItemsHandler = { [weak self] _, _, _, _ in
        // This is necessary to dismiss parent VC
        self?.dismiss(animated: false)
    }
    self.present(shareSheet, animated: true)
}
}

And use it like UIActivityViewController but presenting it without animations

@objc private func shareImage() {
    guard let image = imageView.image else { return }
    let shareVC = ShareViewController(activityItems: [image])
    present(shareVC, animated: false)
}