Search code examples
iosswiftuiviewnsnotificationcenteruiapplicationdelegate

viewDidAppear, but also when app returns from background


I have a UIView ("content") that is a white view with a large red dot.

When the app first launches, I want the dot to fade in (just a half sec).

Whenever that view appears again (so, user has been on some other screen and comes back to that screen), I want the dot to fade in.

No problem so far

override func viewDidAppear(animated: Bool)
    {
    content.fadeIn()
    }

However whenever the app goes to background, when the user brings it to foreground (assuming that view is showing) I want it to fade in. I do this

func weAreActive(notification: NSNotification) {content.fadeIn()}
func weAreNotActive(notification: NSNotification) {content.alpha = 0;}

override func viewWillAppear(animated: Bool)
    {
    super.viewWillAppear(animated)
    NSNotificationCenter.defaultCenter().addObserver(
        self, selector: #selector(weAreActive(_:)),
        name: UIApplicationDidBecomeActiveNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(
        self, selector: #selector(weAreNotActive(_:)),
        name: UIApplicationDidEnterBackgroundNotification, object: nil)
    }

override func viewDidDisappear(animated: Bool)
    {
    super.viewDidDisappear(animated)
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }

First issue: I'm concerned about using the viewWillAppear/viewDidDisappear pair to register/unregister. Am I wrong? Should I be using another pair -- perhaps viewDidAppear/somethingElse? Or?

Second issue: I'm worried about having both viewDidAppear and 'weAreActive` triggered at the same time (perhaps, when the app first launches for instance)

It seems inelegant. Is there a better way to look for "this view genuinely appears" with causes including all of launch, awake, and conventional viewDidAppear?


Solution

  • First issue: viewDidAppear and viewDidDisappear would be the best places to register/unregister the notifications, for the reason you outlined in your second issue (on first launch, registering UIApplicationDidBecomeActiveNotification in viewWillAppear would cause a duplicate animation.

    Edit: If you are just checking for when the app returns from background then you should use UIApplicationWillEnterForegroundNotification then you can use viewDidAppear or viewWillAppear without having to worry about duplicate calls.

    viewDidAppear and viewWillDisappear would be the best places to run the animations so the user will actually get to see them.

    Second Issue: viewDidAppear/viewWillAppear don't get called when the app returns from background so you don't have to worry about these both being called at the same time.

    In some scenarios it would be nice to have an overall 'viewHasDefinitelyAppeared' but there are quite a few instances where you would want them separate, not all apps want users to see animations when they return from background which wouldn't be possible if all the methods where combined in one.