When I implement UIActivityViewController on SwiftUI with an app that looks like this
Code Block swift import SwiftUI struct ContentView: View { @State private var isSheetPresented:Bool = false var body: some View { Text("Hello, world!") .padding() Button(action: { self.isSheetPresented = true }) { Text("Share") }.sheet(isPresented: $isSheetPresented) { ActivityView(activityItems: ["this is some text that could normally be saved to files, but somehow, once you save it to files, or do something in other specific apps, the share button somehow doesn't work"], applicationActivities: []) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } struct ActivityView: UIViewControllerRepresentable { var activityItems: [Any] var applicationActivities: [UIActivity]? func makeUIViewController(context: UIViewControllerRepresentableContext<ActivityView>) -> UIActivityViewController { UIActivityViewController(activityItems: activityItems, applicationActivities: applicationActivities) } func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ActivityView>) {} }
the share button works but once I save to the text to a file, and go back to my app, the share button doesn't work anymore, I click on it and the share button does nothing, Somehow this doesn't do the same thing with copying the text. I really wonder how to fix this.
I finally ended up finding a solution on my own. You need to set isSheetPresented back to false once you're done with it, else, it doesn't work, maybe because isPresented looks for change since the last call.
Once you change it back to false through the completionHandler, the problem is fixed.
Once you change it back to false through the completionHandler, the problem is fixed.
Code Block `swift import SwiftUI struct ContentView: View { @State private var isSheetPresented:Bool = false var body: some View { Text("Hello, world!") .padding() Button(action: { self.isSheetPresented = true }) { Text("Share") }.sheet(isPresented: $isSheetPresented) { ActivityView(isSheetPresented:$isSheetPresented,activityItems: ["this is some text that could normally be saved to files, but somehow, once you save it to files, or do something in other specific apps, the share button somehow doesn't work"], applicationActivities: []) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } struct ActivityView: UIViewControllerRepresentable { @Binding var isSheetPresented:Bool var activityItems: [Any] var applicationActivities: [UIActivity]? func makeUIViewController(context: UIViewControllerRepresentableContext<ActivityView>) -> UIActivityViewController { let ac = UIActivityViewController(activityItems: activityItems, applicationActivities: applicationActivities) ac.completionWithItemsHandler = {(activityType: UIActivity.ActivityType?, completed: Bool, arrayReturnedItems: [Any]?, error: Error?) in isSheetPresented = false;} return ac; } func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ActivityView>) {} }