Home > Mobile >  update data on screen if app open from mobile settings or from background
update data on screen if app open from mobile settings or from background

Time:10-01

i'm trying to update my app settings screen after reopen from setting or home let me explain in detail what i want to do. in my app setting screen i have to check either notification is enable or disable for my app then i need to show switch button for status. if notifications are disabled then on switch toggle app will move to mobile settings then user can change / update the notification status. now my question is if user reopen the app by pressing app name from top left corner of mobile screen or press home button then again open app i need to update my switch button status without moving back and reload the setting screen.

CodePudding user response:

This following solution may helps you. you can do this using Local Notification when app becomes active.

what you have to do is following first you need to add observer in your settings view controller

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
}

}

then in your SceneDelegate you have to post a notification which you add in settings screen

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.
}

Hope this may helps you best of luck

  • Related