Skip to content

Instantly share code, notes, and snippets.

@syxc
Forked from yoni-g/closeAppElegantly.swift
Created July 12, 2022 01:31
Show Gist options
  • Save syxc/1f6da535f68bcb98f51a86a9649abb38 to your computer and use it in GitHub Desktop.
Save syxc/1f6da535f68bcb98f51a86a9649abb38 to your computer and use it in GitHub Desktop.
How to exit an iOS app without it looking like a crash? - Swift
func showMessageResetApp(){
let exitAppAlert = UIAlertController(title: "Restart is needed",
message: "We need to restart the app on your first login to the app.\n Please reopen the app after this.",
preferredStyle: .alert)
let resetApp = UIAlertAction(title: "Close Now", style: .destructive) {
(alert) -> Void in
// home button pressed programmatically - to thorw app to background
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
// terminaing app in background
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
exit(EXIT_SUCCESS)
})
}
let laterAction = UIAlertAction(title: "Later", style: .cancel) {
(alert) -> Void in
self.dismiss(animated: true, completion: nil)
}
exitAppAlert.addAction(resetApp)
exitAppAlert.addAction(laterAction)
present(exitAppAlert, animated: true, completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment