ViewDidAppear is called when the app is launched due to a significant location change, even though it is not visible and in the background
I noticed that it viewDidAppear
gets called when my app starts (in the background) due to a significant change in location, even if the screen is off and the app is definitely not visible (I was on the home screen, and the screen is anyway).
In other words, the view did not really appear. I can figure out what viewDidLoad
will be called because this view controller will get an instance in didFinishLaunchingWithOptions
, which is what is being called in this case.
But why is the call being viewDidAppear
called if it doesn't appear on the screen (the display is off and I was on the home screen anyway)?
Does iOS always call viewDidAppear
after viewDidLoad, regardless of whether it actually appears?
If so, how can I differentiate between the visible view and the described case where it is not actually displayed?
source to share
You can use the property UIApplication
applicationState
to differentiate between when it is called viewDidAppear
when the background starts and when the view is actually displayed on the user's screen. For example:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
let application: UIApplication = UIApplication.sharedApplication()
switch application.applicationState {
case .Active:
// Do something when the view actually appears.
case .Inactive:
// The app is briefly Inactive when an it launched from
// "not running/terminated". viewDidAppear might be called while the
// applicationState property is still set to this.
case .Background:
// Don't do anything because the view didn't really appear on screen
// for the user.
}
}
This looks like a trick to me where my app is started in the background with locationManagerDelegate.locationManager(didVisit)
. I'm sure this will be the same for significant location changes.
source to share