quit in swift

To quit or exit an iOS app in Swift, you can use the exit(0) function. Here is an example of how to use it:

main.swift
func quitApp() {
    UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
        exit(0)
    })
}
217 chars
7 lines

This function sends an action to suspend the app's URL session task, waits for 1 second using a DispatchQueue, then calls the exit(0) function to terminate the app. Note that calling the exit(0) function is generally not recommended, as it can be seen as a "force quit" and may lead to a negative user experience. It's better to let the user close the app through the normal iOS UI.

gistlibby LogSnag