【问题标题】:How to open Share Sheet from presented sheet如何从提供的工作表中打开共享工作表
【发布时间】:2021-12-10 02:25:50
【问题描述】:

按照几个教程 (Medium for example),您可以打开这样的共享表:

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 获得解决方案)

【问题讨论】:

    标签: ios swift swiftui ios-sharesheet


    【解决方案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)
            }
        }
    }
    

    【讨论】:

    • 像魅力一样工作。感谢您提供非常详尽的解决方案和解释!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多