UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

shareActivity with IOS 15.0

Forums > SwiftUI

Hi, i had a code that worked perfectly with IOS 14, and now gives yellow warning + crash on IOS 15. It is for simply have a popup to share things (links, image, pdf... whatever). The code :

// for windowLevel, i used 0 if the view were at the top of the app, 1 if it was in a view that was allready a sheet for example.
DispatchQueue.main.async {
            if let vc = UIApplication.shared.windows[windowLevel].rootViewController {
                shareActivity.popoverPresentationController?.sourceView = vc.view
                shareActivity.popoverPresentationController?.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height, width: 0, height: 0)
                shareActivity.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down
                vc.present(shareActivity, animated: true, completion: nil)
            }
        }

This crash now on IOS 15, and before compile appends a yellow warning that says "'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead".

I understand that it is for multi opened windows reasons, I tried a lot of things but never succeed to apply the warning advise. Is there anyone who has an easy way to use shareActivity on SwiftUI ? Thanks !

3      

I found this : https://swiftui.gallery/uploads/code/ShareSheet.html It is the better solution for now, works fine. It is just that sometimes i have to dismiss my view just before to use it if i am allready in a .sheet, but that's better than nothing. If anyone has better, i take !

3      

Hi You could try

let allScenes = UIApplication.shared.connectedScenes
let scene = allScenes.first { $0.activationState == .foregroundActive }

if let windowScene = scene as? UIWindowScene {
// code to run Share Sheet
}

3      

Thanks @NigelGee, i had search on this way, but the part that i don't succeed to deal with is "// code to run Share Sheet" 😅. I don't understand how it works to append the shareActivity on the windowScene, I never succeeded in the next step.

3      

@linkiliz Here we are! ☺️

Not ideal, but till a better solutions comes along...

func shareSheet(url: String) {
    let url = URL(string: url)
    let activityView = UIActivityViewController(activityItems: [url!], applicationActivities: nil)

    let allScenes = UIApplication.shared.connectedScenes
    let scene = allScenes.first { $0.activationState == .foregroundActive }

    if let windowScene = scene as? UIWindowScene {
        windowScene.keyWindow?.rootViewController?.present(activityView, animated: true, completion: nil)
    }

}

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.