Search code examples
swiftswiftuiuialertview

How can I update my app code using SwiftUI 2 where there is no SceneDelegate


On my app I used to present programmatically an UIAlert with the following code:

debugPrint("didReceiveInvitationFromPeer")
        
        let fieldAlert = UIAlertController(title: "Richiesta di conssessione",
                                           message: "da parte di \(peerID.displayName)",
            preferredStyle: .alert)
        
        fieldAlert.addAction( UIAlertAction(title: "Rifiuta", style: .cancel) { (action) in
            invitationHandler(false, nil)
        } )
        
        fieldAlert.addAction( UIAlertAction(title: "accetta", style: .default) { (action) in
            invitationHandler(true, self.session)
            
            delay(2) {
                // do stuff
            }
        } )

if let scene = UIApplication.shared.connectedScenes.first,
            let sd : SceneDelegate = (scene.delegate as? SceneDelegate) {
            
            sd.window?.rootViewController?.present(fieldAlert, animated: true, completion: nil)
        }

but now SceneDelegate not exists anymore, is there any solution to how can present the alert?


Solution

  • No need SceneDelegate, you can directly present by this

    UIApplication.shared.windows.first?.rootViewController?.present(fieldAlert, animated: true, completion: nil