3

For SwiftUI, when you want to present the ActivityViewController you can do with 3 steps like the code below

Deprecated Code

guard let urlShare = URL(string: "https://apple.com") else { return }
let activityVC = UIActivityViewController(activityItems: [urlShare], applicationActivities: nil)

// 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)

There are similar questions

But in the case of presenting the UIActivityViewController, the view doesn't not present at all, and it presents the error log

UIApplication.shared.connectedScenes
        .filter({$0.activationState == .foregroundActive})
        .map({$0 as? UIWindowScene})
        .compactMap({$0})
        .first?.windows
        .filter({$0.isKeyWindow})
        .first?.rootViewController?.present(activityVC, animated: true, completion: nil)
[Presentation] Attempt to present 
<UIActivityViewController: 0x10f015e00> on 
<_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10290e570> 
(from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10290e570>) 
which is already presenting 
<_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x10a923870>.

What is the equivalent of this code in iOS 15?

UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
Chat Dp
  • 555
  • 5
  • 15
  • https://stackoverflow.com/questions/68380346/swiftui-how-to-implement-half-screen-sharesheet/68382332#68382332 – lorem ipsum Jul 26 '21 at 15:07
  • 1
    It likely doesn't present because you are doing it from a sheet/modal. You can try putting it in a popover like the link above – lorem ipsum Jul 26 '21 at 15:19

0 Answers0