How to check that user is back from Settings

不打扰是莪最后的温柔 提交于 2020-08-08 06:21:40

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/39464439/how-to-check-that-user-is-back-from-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!