0

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.'

An additional note: it runs fine when simulating on an iPhone. The crash only occurs when running on an iPad simulator or actual iPad.

Ben Long
  • 13
  • 3
  • https://stackoverflow.com/questions/69108197/actionsheet-crashes-on-ipad-not-on-iphone#comment122142654_69108197 – lorem ipsum Sep 12 '21 at 17:33

1 Answers1

0

Found the answer, thanks to this video: https://www.youtube.com/watch?v=kBrPnXzjgCQ

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)
Ben Long
  • 13
  • 3