1

按照几个教程(例如 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 获得解决方案)

4

1 回答 1

1

问题在于试图从根控制器显示第二张表。它已经附加了一个,所以拒绝另一个(在这种情况下是 Activity)。

解决方案是使用自己的控制器,放置在打开的工作表中作为活动的演示者。

这是一个简单的演示。使用 Xcode 13 / iOS 15 测试。

演示

struct SheetView: View {

   @Binding var showSheetView: Bool
    @State private var isShare = false
   var body: some View {


      // Below share Sheet - now works!
      Button(action: {
            isShare = true    // present activity
      }) {
        Text("Share Me")
      }
      .background(SharingViewController(isPresenting: $isShare) {
         let url = URL(string: "https://apple.com")
         let av = UIActivityViewController(activityItems: [url!], applicationActivities: nil)
         av.completionWithItemsHandler = { _, _, _, _ in
                isShare = false // required for re-open !!!
            }
            return av
        })
   }
}

struct SharingViewController: UIViewControllerRepresentable {
    @Binding var isPresenting: Bool
    var content: () -> UIViewController

    func makeUIViewController(context: Context) -> UIViewController {
        UIViewController()
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        if isPresenting {
            uiViewController.present(content(), animated: true, completion: nil)
        }
    }
}
于 2021-10-24T05:23:11.110 回答