UIActivityViewController in SwiftUI on Mac opens blank window

I'm creating an iOS app with SwiftUI and everything works great on all platforms including OSX except for the UIActivityViewController. The controller works as expected on iOS but just displays a blank window on OSX that I can't even close. I'm passing simple text to the controller to share via email or printer.


Here is the controller struct;


struct ActivityViewController: UIViewControllerRepresentable {

    var activityItems: [Any]
    var applicationActivities: [UIActivity]? = nil

    func makeUIViewController(context: UIViewControllerRepresentableContext) -> UIActivityViewController {
        let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: applicationActivities)
        return controller
    }

    func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext) {}

}


And I present it like this;


.sheet(isPresented: $bShare, onDismiss: {
            print("Dismiss")
        }, content: {
            Text("It's Fun to Share")
            ActivityViewController(activityItems: ["place holder"])
        })
I'm having the same problem, did you ever find a solution?
I'm seeing the same issue but on iOS (iOS 14 beta 5). Here's the code I'm currently testing with, found it somewhere on the interwebz so I take no credit for it.

Code Block language
import SwiftUI
struct ShareSheetView: UIViewControllerRepresentable {
    typealias Callback = (_ activityType: UIActivity.ActivityType?, _ completed: Bool, _ returnedItems: [Any]?, _ error: Error?) -> Void
    let activityItems: [Any]
    let applicationActivities: [UIActivity]? = nil
    let excludedActivityTypes: [UIActivity.ActivityType]? = nil
    let callback: Callback? = nil
    
    func makeUIViewController(context: Context) -> UIActivityViewController {
        let controller = UIActivityViewController(
            activityItems: activityItems,
            applicationActivities: applicationActivities)
        controller.excludedActivityTypes = excludedActivityTypes
        controller.completionWithItemsHandler = callback
        return controller
    }
    func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
        // nothing to do here
    }
}
struct ShareSheetView_Previews: PreviewProvider {
    static var previews: some View {
        ShareSheetView(activityItems: [URL(string: "https://volante.io")!])
    }
}

  • * Edit:

I just noticed that the ivar activityItems is an array of Any, which let me send a URL wrapped in a Binding<>. This is of course not share:able and this is the reason I was seeing this bug.

Maybe this is the same error you've made? To get it working I updated the code to this:
Code Block
ShareSheetView(activityItems: [self.filePath.unsafelyUnwrapped])


I use unsafelyUnwrapped because I know the filePath property will always be set.
UIActivityViewController in SwiftUI on Mac opens blank window
 
 
Q