From iOS 15.4.0 we have the crash on Crashlytics but don't known how to reproduce and fix it. It is producing crash due to Apple default shareSheet. I hope someone can provide some insights to fix this.
Code I use to open shareSheet
func shareFiles(rootVC: UIViewController, items: [Any]) {
let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
// This lines is for the popover you need to show in iPad
activityViewController.popoverPresentationController?.sourceView = rootVC.view
// This line remove the arrow of the popover to show in iPad
activityViewController.popoverPresentationController?.permittedArrowDirections = .down
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: UIScreen.main.bounds.width/2,
y: UIScreen.main.bounds.height,
width: 0, height: 0)
DispatchQueue.main.async {
rootVC.present(activityViewController, animated: true, completion: nil)
}
}
CodePudding user response:
One of our applications was producing the exact same crash on production and We've made a workaround for the crash.
You can create a CustomShareSheet class that extends UIActivityViewController and this class will dismiss the CustomShareSheetController when the application will resign from its active state, thus the crash won't occur anymore.
class CustomShareSheet: UIActivityViewController {
override func viewDidLoad() {
debugPrint("CustomShareSheet \(#function)")
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(notification:)), name: UIApplication.willResignActiveNotification, object: nil)
}
@objc private func applicationWillResignActive(notification: NSNotification) {
self.dismiss(animated: true)
debugPrint("\(#function) || dismissed")
}
}
===========================
class Helper {
class func shareFiles(rootVC: UIViewController, items: [Any]) {
debugPrint("\(#function) || itemCount: \(items.count)")
let customShareSheet = CustomShareSheet(activityItems: items, applicationActivities: nil) // Uses of CustomShareSheet
// This lines is for the popover you need to show in iPad
customShareSheet.popoverPresentationController?.sourceView = rootVC.view
// This line remove the arrow of the popover to show in iPad
customShareSheet.popoverPresentationController?.permittedArrowDirections = .down
customShareSheet.popoverPresentationController?.sourceRect = CGRect(x: UIScreen.main.bounds.width/2,
y: UIScreen.main.bounds.height,
width: 0, height: 0)
DispatchQueue.main.async {
rootVC.present(customShareSheet, animated: true, completion: nil)
}
}
}
===========================
var _items = [Any]()
Helper.shareFiles(rootVC: yourCurrentViewController, items: _items)