Is there any way to restart my iOS app programmatically?

Viewed 3575

I would like to know if is any method to restart my app programmatically. It's an iOS app and I work with Xcode 12.3 in swift.

1 Answers

There are no methods available in iOS to restart your application, BUT you could manually reinstantiate initial root UIViewController when needed.


To reinstantiate root UIViewController you could use following static functions in your AppDelegate class:

static func getWindow() -> UIWindow? {
    return UIApplication.shared.keyWindow
}

static func resetViewController() {
    let window = getWindow()
    
    let controller = ...  /// Instantiate your inital `UIViewController`
    
    window?.rootViewController = controller
    window?.makeKeyAndVisible()
}

Note:

"Restarting" app is a very uncommon practice and should be avoided in all cases if possible.

Related