Wednesday, September 20, 2023
HomeiOS Developmentios - open Share Sheet from introduced sheet

ios – open Share Sheet from introduced sheet

[ad_1]

Following several tutorials (Medium for example) you can open a share sheet like this:

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")
}

However, this does not work within a presented sheet.

BaseView, with example of working share sheet and code to open Sheet:

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")
      }
   }

}

Opened SheetView, where same code does not work:

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")
      }
   }
}

How to accomplish showing share sheet from within sheetview? Does the intent to share something have to be passed to BaseView (and/ or does SheetView have to be closed) and then called a share sheet from there? (I would appreciate a solution directly from SheetView)

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments