Creating a Share Sheet in an onDismiss handler in iOS

I'm trying to come out of a Preferences sheet and then immediately show a Share sheet (since it appears to be impossible to show a Share sheet from within another sheet.) I'm trying to call the Share sheet from the onDismiss handler of the Preferences sheet, like so:

Button("Prefs") {
     theCurrentLog.prefsAreVisible.toggle()
     }
     .sheet(isPresented:$theCurrentLog.prefsAreVisible, onDismiss: {
          guard let data = currentPrefs.exportURL else { return }
          let av = UIActivityViewController(activityItems: [data], applicationActivities: nil) 
  UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)        
}) {
            Prefs(currentPrefs: $currentPrefs)
        }

However, this causes a crash with the following error: 'UIPopoverPresentationController (<UIPopoverPresentationController: 0x15edddf10>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'

I have no idea how to specify a non-nil sourceView.

On additional note: it works fine when I simulate it on an iPhone. It's only the iPad simulators that crash. It also crashes running on an actual iPad.

Found the answer:

if UIDevice.current.userInterfaceIdiom == .pad {
                av.popoverPresentationController?.sourceView = UIApplication.shared.windows.first
                av.popoverPresentationController?.sourceRect = CGRect (
                    x: UIScreen.main.bounds.width / 2.1,
                    y: UIScreen.main.bounds.height / 2.3,
                    width: 200, height: 200)
Creating a Share Sheet in an onDismiss handler in iOS
 
 
Q