2

In order for the ViewController to "do something specific" just before the app goes into the background state...

I am to understand that this sort of thing is generally handled inside the applicationWillResignActive(_:) method, but this method resides inside the AppDelegate class, not the ViewController.

This is my first time doing lifecycle related stuff on IOS, and so I'm not sure whether to:

1) Call a ViewController method from inside the AppDelegate class. This would mean that I have to change the method from private to public.

2) Have the ViewController implement UIApplicationDelegate

PS - Is it okay to just delete the AppDelegate class as long as the ViewController implements UIApplication delegate instead?

EDIT: I should add that this is a single-page app with only one view controller (well, I suppose it will have a settings view controller eventually... but the 'ViewController' that I am referring to will never be popped off the stack).

Thanks!

Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
  • 2
    I'd think a view controller is just that - a class that controls it's "root" view. By that I mean you *should not* have it "do something specific" with regard to the app going into the background. Instead, design your app to (a) have a view controller "do something specific" when it's `viewWillDisappear` and have your app "do something specific" when the `applicationWillResignActive`. –  Feb 13 '18 at 10:35
  • 1
    You can use NotificationCenter or you can implement a method in your AppDelegate to get the current visible viewController and call a custom method on it, a better approach for this last method will be use a BaseViewController for all our App viewControllers – Reinier Melian Feb 13 '18 at 10:35
  • Use notification named as UIApplicationWillResignActiveNotification. This will notify when app going to resignActive state. – Sagar Chauhan Feb 13 '18 at 10:35
  • dfd the ViewController will never be dismissed. It is a single page app with only one ViewController. – Nerdy Bunz Feb 13 '18 at 11:00
  • @Reinier Melian please see edit – Nerdy Bunz Feb 13 '18 at 11:05

2 Answers2

1

Generally you shouldn't delete the AppDelegate unless you have a really good reason. This isn't a good reason.

For your scenario I would investigate using NotificationCenter to observe the UIApplicationWillResignActive event. This event is fired every time the application will enter the background.

For more information see: Apple Docs

e.g.

func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated: animated)
    NotificationCenter.default.addObserver(self, selector: #selector(youFunction), name: .UIApplicationWillResignActive, object: nil)
}

func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated: animated)
    NotificationCenter.default.removeObserver(self)
}
Ollie
  • 1,926
  • 1
  • 20
  • 35
  • like dfd is suggesting above, now I am thinking maybe I was asking the wrong question... and that I might as well "do something specific" in viewWillDisappear since that is presumably called at the same time as the applicationWillResignActive(_:) method anyway. :T – Nerdy Bunz Feb 13 '18 at 10:43
  • That's not technically true, `viewWillDisappear` is used for ViewController navigation and not UIApplication events. If you add a breakpoint within `viewWillDisappear` and press the home button you'll see it's not triggered. – Ollie Feb 13 '18 at 10:50
  • yep I'm doing that right now... and googling "viewWillDisappear not called" :S – Nerdy Bunz Feb 13 '18 at 10:56
  • Yep, so that's where my answer comes in. You should observe the `. UIApplicationWillResignActive` notification from `NotificationCenter` to be alerted when the user presses the home button. – Ollie Feb 13 '18 at 10:57
  • I understand.. forgive me.. I appreciate your answer... I'm just waiting to see if anyone approves any of the other options suggested in the original question. – Nerdy Bunz Feb 13 '18 at 11:17
1

Use NotificationCenter

In YourViewController

class YourViewController : UIViewController {

override func viewDidAppear(_ animated: Bool) {
       NotificationCenter.default.addObserver(self, selector: #selector(reloadTableData(_:)), name: .UIApplicationWillResignActive, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self, name: .UIApplicationWillResignActive, object: nil)
    }
}

func reloadTableData(_ notification: Notification) {
}
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29