如果从移动设置或后台打开应用程序,则更新屏幕上的数据
Posted
技术标签:
【中文标题】如果从移动设置或后台打开应用程序,则更新屏幕上的数据【英文标题】:update data on screen if app open from mobile settings or from background 【发布时间】:2021-09-30 11:51:08 【问题描述】:我正在尝试在从设置或主页重新打开后更新我的应用设置屏幕,让我详细解释一下我想要做什么。 在我的应用程序设置屏幕中,我必须检查我的应用程序的通知是启用还是禁用,然后我需要显示切换按钮的状态。 如果通知被禁用,则开关切换应用程序将移至移动设置,然后用户可以更改/更新通知状态。 现在我的问题是 如果用户通过按手机屏幕左上角的应用名称或按主页按钮重新打开应用,然后再次打开应用,我需要更新我的开关按钮状态而不返回并重新加载设置屏幕。
【问题讨论】:
【参考方案1】:以下解决方案可能会对您有所帮助。 您可以在应用激活时使用本地通知来执行此操作。
你要做的就是跟随 首先你需要在你的设置视图控制器中添加观察者
class SettingsViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
//add Local notification in your controller
NotificationCenter.default.addObserver(self, selector: #selector(updateSettings(notification:)), name: NSNotification.Name(rawValue: "settings"), object: nil)
// Do any additional setup after loading the view.
@objc func updateSettings(notification: NSNotification)
// here you can check your changes like notifications are enabled or not and update your switch button status
然后您必须在 SceneDelegate 中发布您在设置屏幕中添加的通知
func sceneDidBecomeActive(_ scene: UIScene)
NotificationCenter.default.post(name: Notification.Name(rawValue:"settings"), object: nil, userInfo:nil)
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
希望这可以帮助你好运??
【讨论】:
哦,是的,我知道你让我试试以上是关于如果从移动设置或后台打开应用程序,则更新屏幕上的数据的主要内容,如果未能解决你的问题,请参考以下文章