按照几个教程(例如 Medium),您可以打开这样的共享表:
Button(action: {
let url = URL(string: "https://apple.com")
let av = UIActivityViewController(activityItems: [url!], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
}) {
Text("Share Me")
}
但是,这在提供的工作表中不起作用。
BaseView,带有工作共享表和打开表的代码示例:
struct BaseView: View {
...
@State var isSheetViewShowing = false
...
var body: some View {
// Open sheet view where share sheet does not work.
Button(action: {
isSheetViewShowing.toggle()
}) {
Text("Show sheet view")
}
Text("")
.frame(width: 0, height: 0)
.sheet(isPresented: $isSheetViewShowing) {
SheetView(showSheetView: $isSheetViewShowing)
}
// Below share Sheet works!
Button(action: {
let url = URL(string: "https://apple.com")
let av = UIActivityViewController(activityItems: [url!], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
}) {
Text("Share Me")
}
}
}
打开 SheetView,其中相同的代码不起作用:
struct SheetView: View {
...
@Binding var showSheetView: Bool
...
var body: some View {
// Below share Sheet does not work anymore!
Button(action: {
let url = URL(string: "https://apple.com")
let av = UIActivityViewController(activityItems: [url!], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
}) {
Text("Share Me")
}
}
}
如何完成从 sheetview 中显示共享表?是否必须将共享某些内容的意图传递给 BaseView(和/或 SheetView 是否必须关闭),然后从那里调用共享表?(我希望直接从 SheetView 获得解决方案)