Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

swift - 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?

question from:https://stackoverflow.com/questions/65912280/how-can-i-update-my-app-code-using-swiftui-2-where-there-is-no-scenedelegate

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No need SceneDelegate, you can directly present by this

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...