This is now available in a dedicated package: ActivityView
Alternatively my SwiftUI Backports now includes a more complete implementation of ShareLink
that's also more performant.
This is now available in a dedicated package: ActivityView
Alternatively my SwiftUI Backports now includes a more complete implementation of ShareLink
that's also more performant.
Thank you for this code. Nice way to present a UIActivityViewController... why it is that hard in SwiftUI is beyond me 🤷♂️...
I extended it to have a binding for the activityItems
... here are my changes:
@Binding var activityItems: [Any]
init(isPresented: Binding<Bool>, items: Binding<[Any]>,
activities: [UIActivity]? = nil,
func updateUIViewController(_ uiViewController: ActivityViewControllerWrapper, context: Context) {
uiViewController.isPresented = $isPresented
uiViewController.activityItems = $activityItems
final class ActivityViewControllerWrapper: UIViewController {
var activityItems: Binding<[Any]>
let controller = UIActivityViewController(activityItems: activityItems.wrappedValue,
Why did you need a binding @chbeer? A binding only makes sense if the controller for example can modify the items itself, which then needs to be reflected in the UI. But given this isn't possible with a share sheet directly I don't see the point of adding a binding here – unless I'm missing something?
Maybe I‘m holding it wrong, but in my case the item got created in the controller, then given to the share sheet which is then shown. It‘s a generated URL in my case
Ah, with controller you mean the controller that opens the share sheet 🤦♂️ You‘re right! No binding necessary (my old UIKit knowledge shows through)
👍
This is great.
I noticed there's a short freezing behavior(1 or 2 second)s before showing activity view
This is great.
I noticed there's a short freezing behavior(1 or 2 second)s before showing activity view
I think that's typical of UIActivityViewController
. It certainly takes its time with me, regardless of app.
@JetForMe / @skywalkerlw – that’s true but it can be considerably worse for larger assets for example. There are APIs for improving the performance here, checkout the docs on LinkPresentation and that should help you! 👍🏻
UIActivityItemSource
and sending over metadata with this setup, any pointers? Most probably due to the way I have set things upvar body: some View {
GeometryReader { proxy in
VStack() {
}
.onTapGesture {
self.isPresented = true
// err, may be I could fetch metadata here :/
}
.background(Group {
switch self.type {
case .link:
ActivityView(isPresented: self.$isPresented, metadata: self.$currentMetadata or not required for custom meta data, items: [self or whatever items I want to send over for custom meta data])
case .snapshot(let image, _):
ActivityView(isPresented: self.$isPresented, metadata: self.$currentMetadata or not required for custom meta data, items: [self or whatever items I want to send over for custom meta data])
}
})
}
}
I'm finding I don't know the activityItems
when the button is tapped. I have to make a network call to Firebase (ugh) to generate a link, and only then present the activity controller. At first I decided to make the activityItems
an @autoclosure
, and then pass them in as @State
properties, but I don't want setting those to re-create my view. I’m not sure what the best thing to do here is. I don't really want to start generating the link until after the user taps the share button in the first place.
@chbeer @skywalkerlw @lahariganti @JetForMe
Just to let you know I'm now officially supporting this (and many other) SwiftUI views via a dedicated organisation, SwiftUI+.
If you can create any Issues on the new repo then I can respond and track them appropriately. I will respond much more quickly and can help you more effectively.
Also note, the API has some improvements and more improvements are on the way.
In terms of performance Activity controller's do take a second to start and even longer in DEBUG
builds, so if you run on device, then stop the debugger. Re-run the app from the icon on your device and re-test that way (as a user would) and I think you'll find the speed of the presentation is far better.
That being said, there are other things you may can do to improve, namely implementing the LinkPresentation
APIs if you can, since those load asynchronously (alongside the presentation itself). But please create Issues on the new repo to continue this conversation if needed, as I will no longer be checking them here.
Thanks.
Shaps
That's great, thanks!
👍
My new library, SwiftUI Backports now includes a more complete implementation of ShareLink
that's also more performant.
This will be available in the next release.
Note how the
ActivityView
is added as abackground
rather than via a modifier. This is intentional to get around some interoperability issues between UIKit and SwiftUI.Also, popovers on iPad will 'just work' and even nested
ActivityView
's work without issue 👍