首页 > 解决方案 > 在 SwiftUI 的模态 .sheet() 中显示共享表(UIActivityViewController)

问题描述

我正在尝试从 SwiftUI 中的模态 .sheet() 中显示共享表。

在此处输入图像描述

共享表不显示;我收到以下错误:

[Presentation] Attempt to present <UIActivityViewController: 0x105012200> on <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10110b070> (from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10110b070>) which is already presenting <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x1038060d0>.
[ShareSheet] connection invalidated

如何使它从 .sheet() 显示

这是我的代码:

import SwiftUI

struct ContentView: View {
    
    @State private var displayModal = false
    
    var body: some View {
        VStack {
            Button("1. Share Sheet") {
                shareSheet()
            }.padding()
            
            Button("2. Modal Sheet") {
                displayModal.toggle()
            }
            .sheet(isPresented: $displayModal) {
                Button("3. Inside Modal Sheet") {
                    shareSheet()
                }
            }
            .padding()
        }
        
    }
    
    func shareSheet() {
        let items: [Any] = ["Share My App", URL(string: "https://www.apple.com")!]
        let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
        UIApplication.shared.windows.first?.rootViewController?.present(ac, animated: true, completion: nil)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

多谢你们。

标签: swiftuiios-sharesheet

解决方案


推荐阅读