How to restart an app when resumes from multi-tasking
Asked Answered
P

3

1

So my app relies on an internet connection and every time the app is opened from the home screen it checks using Reachability and then allows the user to continue or tells them to get connected. My issue however is that when the app is resumed from multitasking, it skips the initial view controller and goes straight to where they were. Is there any way in swift that I could get the app to refresh or restart when it is resumed from multi-tasking?

Thanks

EDIT When using this code:

let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
self.window.rootViewController = MainStoryboard.instantiateInitialViewController()

I get expected declaration error. enter image description here

Pervasive answered 4/6, 2015 at 14:44 Comment(0)
C
7

You can set your root view controller and it will restart you main VC. Assuming your using a storyboard:

(self is referring to your AppDelegate)

Swift:

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
self.window.rootViewController = storyboard.instantiateInitialViewController()

Objective-C:

self.window.rootViewController = [[UIStoryboard storyboardWithName:@"MyStoryboardName" bundle:nil] instantiateInitialViewController];
Cockneyism answered 4/6, 2015 at 15:2 Comment(2)
Ok and I would put this in the app delegate file? Also how do I set my root view controller? @CockneyismPervasive
I suggest you would. The root vc is a property of the app delegate.Cockneyism
P
2

I just added the following in the plist: "Application does not run in background" and set it to "YES"

Seem to be working.

Pervasive answered 4/6, 2015 at 19:19 Comment(2)
Im sure thats the best option.Copybook
@KarloA.López Yeah - just found it. Do you know if this could have an effect on the debug console as now when my app is offline it isn't printing 'you're offline' like it should...Pervasive
M
0

You can use navigation controller:

    let vc = self.storyboard!.instantiateViewController(withIdentifier: "FirstPageViewController") as! FirstPageViewController
    self.navigationController?.pushViewController(vc, animated: true)
Morocco answered 13/10, 2017 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.