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
  • 21
  • 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

-1

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
  • 21
  • 3
  • Perfect example of why links in Stack Overflow questions are often a negative thing. The link is now 404ing and the author of this answer gave very little details about this answer and why it works. Along with formatting the code very poorly, the author gave no details about where this code should be placed, why the values (2.1, 2.3 & 200) were chosen. And no details about why `UIDevice.current.userInterfaceIdiom == .pad` was used here. – Charlie Fish Jun 27 '23 at 02:19