I am trying to create a share sheet to share a Text
, it was working fine in iOS 14 but in iOS 15 it tells me that
‘windows’ was deprecated in iOS 15.0: Use UIWindowScene.windows on a
relevant window scene instead.
how can I make it work on iOS 15 with SwiftUI
Button {
let TextoCompartido = "Hola ๐ "
let AV = UIActivityViewController(activityItems: [TextoCompartido], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(AV, animated: true, completion: nil)
}
5
Answers
you could try the following using the answer from: How to get rid of message " 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead" with AdMob banner?
Note that your code works for me, but the compiler give the deprecation warning.
To avoid warning, change the way you retrieve the window scene.
Do the following:
I think you would be best served using SwiftUI APIs directly. Generally, I would follow these steps.
View
namedActivityView
that adheres toUIViewControllerRepresentable
. This will allow you to bringUIActivityViewController
to SwiftUI.Identifiable
struct to contain the text you’d like to display in theActivityView
. Making this type will allow you to use the SwiftUI sheet API and leverage SwiftUI state to tell the app when a newActivityView
to be shown.@State
variable that will hold on to yourIdentifiable
text construct. When this variable changes, the sheet API will perform the callback.ActivityView
which will be presented to your user.The code below should help get you started.
iOS 16 includes the
ShareLink
view which works like this:Source: https://developer.apple.com/videos/play/wwdc2022/10052/
Time code offset: 25 minutes 28 seconds
Tested in in iOS 15 with SwiftUI
To avoid iOS 15 method deprecation warning use this extension