0

I’m using the following code that gets triggered via a button to display the share sheet in my app:

    func shareSheet() {
           guard let urlShare = URL(string: "https://google.com") else { return }
           let activityVC = UIActivityViewController(activityItems: [urlShare], applicationActivities: nil)
           UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
       }

This code brings up the warning:

'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead

I don't understand how I can get rid of it. Already checked the approach suggested here 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? but it's not working. Any ideas how I can bypass the warning?

Many Thanks!

SwiftUIRookie
  • 591
  • 7
  • 16

1 Answers1

1

You can use following as UIApplication.shared.currentUIWindow()?.rootViewController

public extension UIApplication {
    func currentUIWindow() -> UIWindow? {
        let connectedScenes = UIApplication.shared.connectedScenes
            .filter({
                $0.activationState == .foregroundActive})
            .compactMap({$0 as? UIWindowScene})
        
        let window = connectedScenes.first?
            .windows
            .first { $0.isKeyWindow }

        return window
        
    }
}
cluelessCoder
  • 948
  • 6
  • 19
  • Thanks, this works but do you think this is the intended way? I guess it just bypasses the error but still uses Apples old way of using it. The recommendation is to use UIWindowScene.windows but I don't get how this is supposed to work. – SwiftUIRookie Dec 10 '21 at 11:59
  • 1
    No, this is the intended way - if you look closely, it uses UIWindowScene.windows. – cluelessCoder Dec 10 '21 at 13:00