swift ios – How to run function in ViewController from AppDelegate

I am trying to run a function in certain ViewController using AppDelegate

func applicationDidBecomeActive(_ application: UIApplication) {

But somehow the function does not seem to run at all when the app has become active after entering the app from the background.

  • game exits from pause state after resuming it from background in swift
  • Swift - pushViewController from appDelegate, rootViewController.navigationController is nil
  • Handling different URL Schemes in iOS (Facebook and Instagram)
  • Call class method with argument from another class method
  • How to run timer thought the app entered background or is terminated
  • wrong generic type in swift
  • The function looks like this

    func grabData() {
            self._DATASERVICE_GET_STATS(completion: { (int) -> () in
                if int == 0 {
                } else {
                    for (_, data) in self.userDataArray.enumerated() {
                        let number = Double(data["wage"]!)
                        let x = number!/3600
                        let z = Double(x * Double(int))
                        self.money += z
                        let y = Double(round(1000*self.money)/1000)
                        self.checkInButtonLabel.text = "\(y) KR"
                    self.workingStatus = 1

    And uses this var

    var money: Double = 0.000

    What have I missed?


    3 Solutions Collect From Internet About “swift ios – How to run function in ViewController from AppDelegate”

    ViewController().grabData() will create a new instance of the ViewController and call this function. Then.. as the view controller is not in use it will be garbage collected/removed from memory. You need to be calling this method on the actual view controller that is in use. Not a new instance of it.

    The best option would be to listen for the UIApplicationDidBecomeActive notification that iOS provides.

        selector: #selector(grabData),
        name: NSNotification.Name.UIApplicationDidBecomeActive,
        object: nil)

    make sure that you also remove the observer, this is usually done in a deinit method

    deinit() {

    I simply solved it like this:

    func applicationDidBecomeActive(_ application: UIApplication) {
            let viewController = self.window?.rootViewController as! ViewController

    Make two global variables for example var instance: Viewcontroller? and var flag = 0 then in your viewDidLoad function set instance variable as self like this: instance = self and flag = 1

    Now you are ready to use any function of this Controller like this:

       func applicationDidBecomeActive(_ application: UIApplication) {
                if flag == 1 {

    Maybe it will need a ? after instance (I’m not on my mac right now so I can’t test it) if it needs it just add it.

    Hope it helps.


    To make global variables just add the lines before class

    flag is to avoid to run the function at your first launch