Search code examples
iosswiftalertview

Keeping alert view active when the user leaves and re-enters the app without killing it


I have an alert view which the user get when they press the logout button where they can either choose to log out or cancel however with the alert view active if i leave the app and come back(without killing the app) the alert view disappears but i want it to be still active as the user hasn't chosen cancel or logout yet. is there a way to achieve this? thanks

Well i didn't think putting my code was necessary in this scenario, anyway here's what my function looking like

func displayLogOutAlert() {
    let alert = PCLBlurEffectAlert.Controller(title: nil, message: nil, effect: UIBlurEffect(style: .light), style: .actionSheet)
    let alertBtnYes = PCLBlurEffectAlert.Action(title: "Log Out", style: .destructive, handler: { (action) in
        alert.dismiss(animated: true, completion: nil)
        FBSDKLoginManager().logOut()
        GIDSignIn.sharedInstance().signOut()
        self.logoutIndicator.startAnimating()
        self.performSegue(withIdentifier: "backToLogin", sender: self)
        })
    let alertBtnCancel = PCLBlurEffectAlert.Action(title: "Cancel", style: .cancel, handler: nil)
        //self.presentingViewController?.dismiss(animated: true, completion: nil)
    alert.addAction(alertBtnYes)
    alert.addAction(alertBtnCancel)

    //setting button/text properties
    alert.configure(cornerRadius: 10)
    alert.configure(buttonBackgroundColor: UIColor.white)
    alert.configure(buttonHeight: 55)
    alert.configure(buttonFont: [.destructive: UIFont.systemFont(ofSize: 20), .cancel: UIFont.boldSystemFont(ofSize: 20)])
    alert.show()


}

Solution

  • Use iOS State Restoring concept to acheive this.

    When you go to lunch and leave something on your desk, you expect that everything will remain in place after your return. Your user expect’s the same when he presses the “Home” button on his iPhone or receives a phone call. He expects to open application and find application in the same state, as it was left.

    Application state preserving is often skipped by developers. To make user happy we must care about application state saving and restoring.

    enter image description here

    Here is the link to implementation