swiftuiactivityviewcontrollerswiftuinssharingservice

SwiftUI exporting or sharing files


I'm wondering if there is a good way export or share a file through SwiftUI. There doesn't seem to be a way to wrap a UIActivityViewController and present it directly. I've used the UIViewControllerRepresentable to wrap a UIActivityViewController, and it crashes if I, say, present it in a SwiftUI Modal.

I was able to create a generic UIViewController and then from there call a method that presents the UIActivityViewController, but that's a lot of wrapping.

And if we want to share from the Mac using SwiftUI, is there a way to wrap NSSharingServicePicker?

Anyway, if anyone has an example of how they're doing this, it would be much appreciated.


Solution

  • Since iOS 16 you can use ShareLink:

    ShareLink(item: "http://www.myappurl.com") {
        Label("Share app", systemImage: "square.and.arrow.up")
    }
    

    or just:

    ShareLink(item: "http://www.myappurl.com")