0

I face the problem that my app calls:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions [UIApplicationLaunchOptionsKey: Any]?) -> Bool

then do something and finally calls func applicationDidEnterBackground.

The app actually contains in the app stack. I can switch back to the home screen of the simulator and back into the program. Unfortunately the function viewDidLoad is never called.

How can I debug whats happen and where the app "stops" and call the background function instead of active?

Thanks in advance.

pacification
  • 5,838
  • 4
  • 29
  • 51
StefanN
  • 135
  • 7
  • `viewWillAppear` is probably what you want - `viewDidLoad` is only called when the view is loaded from its nib - which it may not need to do after suspension. – Grimxn Jul 18 '18 at 07:32
  • I don't thing any method called when you come from background if you wish do anything you can do in applicationWillEnterForeground method – Chirag Shah Jul 18 '18 at 07:34
  • @Grimxn i wried but viewWillAppear not called if you come from background – Chirag Shah Jul 18 '18 at 07:34
  • Was it always that way? Is this a new application? If its recent maybe you can look at the change history in git if you have set it up to cover the target/project/workspace to see what made it not start UI anymore. – Fabian Jul 18 '18 at 08:13
  • The Info.plist must reference a Storybord by name, usually “Main.storyboard” and “Main.storyboard” must contain a ViewControllers marked “InitialViewController”. Thats as far as I know the usual way the app figures out what to show in the beginning. Maybe the class of the InitialViewController is not set? If your custom class name is not set in InitialViewController, its method will not be called. – Fabian Jul 18 '18 at 08:16

3 Answers3

0

There are multiple reasons why you app may enter the background, From 'pressing' the home button on the simulator/device to actually calling the suspend method.

Some solutions for debugging: Check in Xcode the debug navigator, add exception breakpoints, use instruments to investigate suspicious behaviours.

Also check App Life Cycle and View Controller Life Cycle

Durdu
  • 4,649
  • 2
  • 27
  • 47
0

ViewDidLoad is never called when app return in foreground. You must setup an observer that is triggered whenever the app come back in foreground and then do what you want to do in your ViewController.

NotificationCenter.default.addObserver(self, 
                                       selector: #selector(funcCalledWhenReturningInForeground), 
                                       name: .UIApplicationWillEnterForeground,
                                       object: nil)

or you can use as the name of observer (is just like the viewWillAppear and viewDidAppear logic)

UIApplicationDidBecomeActive

Insthead, to know when the app is going in background you can use

UIApplicationWillResignActive
MarkWarriors
  • 544
  • 2
  • 9
  • 17
0

In order to detect the app entered in background/Foreground in your ViewController Add observer in your view controller
example

 NSNotification.Name.UIApplicationWillResignActive
devgariya
  • 41
  • 5