I'm trying to present a share sheet using the code below. It works fine on iPhones, but crashes on iPad (both in the simulator and on a real device).
The crash gives the following message:
Thread 1: "UIPopoverPresentationController (<UIPopoverPresentationController: 0x7fedaa285c80>) should have a non-nil sourceView or barButtonItem set before the presentation occurs."
I think it might have to do with the issues mentioned here, but I'm not sure how to incorporate the solutions they discuss into my code.
import SwiftUI
struct ContentView: View {
var body: some View {
Button(action: showSheet) {
Text("Show sheet")
}
}
func showSheet() {
guard let data = URL(string: "https://www.google.com") else { return }
let sheet = UIActivityViewController(activityItems: [data], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(sheet, animated: true, completion: nil)
}
}