4

I am sending local notifications to my users, and I want to show the relevant title on the notification settings button.

If local notifications are off, this title should be "Notifications: off", and if local notifications are on, this title should be something like "Preferences".

Right now I'm checking this in viewDidLoad and viewDidAppear, and it works.

if UIApplication.sharedApplication().currentUserNotificationSettings()?.types.rawValue == 0 {
    //the first title
} else {
    //the second title
}

Except one case. If my user is changing notifications in the phone settings from "on" to "off" or vice versa, and after that he is back — the title is not changing (because this viewController already loaded and did appear).

How could I check that user is back from the Settings?

lithium
  • 1,272
  • 1
  • 14
  • 28
  • Pretty hard to understand your question, what's the Settings you talking about? a vc in your app, or changed setting in setting app then back to yours? – Tj3n Sep 13 '16 at 07:50
  • changing settings in the phone settings then back to mine, yes. – lithium Sep 13 '16 at 07:56

2 Answers2

4

You can observe this notification when your app is come to foreground from inactive, the selector will be called everytime your app is opened again from background:

Put in viewDidLoad:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.reloadData), name: UIApplicationWillEnterForegroundNotification, object: UIApplication.sharedApplication())

Put in viewDidDissapear or deinit:

NSNotificationCenter.defaultCenter().removeObserver(self)
Tj3n
  • 9,837
  • 2
  • 24
  • 35
0

Swift 5.5:

NotificationCenter.default.addObserver(
    self,
    selector: #selector(yourFunction),
    name: UIApplication.willEnterForegroundNotification,
    object: UIApplication.shared
)

And:

deinit {
    NotificationCenter.default.removeObserver(self)
}