How to call a function every time a user opens an application

I want to call a function called update every time a user removes his applicationโ€™s icon and opens it. How should I do it? Which method works, that I can override whenever the application starts, regardless of whether it is killed from the background or the user clicks the home button.

import UIKit class ViewController: UIViewController { @IBOutlet weak var label: UILabel! @IBOutlet weak var button: UIButton! @IBOutlet weak var labeltwo: UILabel! var NSDateDefalt = NSUserDefaults.standardUserDefaults() var date : NSDate? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. date = NSUserDefaults.standardUserDefaults().objectForKey("yourKey") as? NSDate label.text = "\(date)" update() } @IBAction func buttona(sender: AnyObject) { date = NSDate() label.text = "\(date)" NSUserDefaults.standardUserDefaults().setObject(NSDate(), forKey:"yourKey") } func update(){ let now = NSDate() let seconds = now.timeIntervalSinceDate(date!) labeltwo.text = "\(seconds))" } } 

Update: I implemented in my App Delegate

  func applicationWillEnterForeground(application: UIApplication) { ViewController().update() } 
+6
source share
5 answers

You need to implement your logic in AppDelegate

 class AppDelegate: UIResponder, UIApplicationDelegate { func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { // first launch // this method is called only on first launch when app was closed / killed return true } func applicationWillEnterForeground(application: UIApplication) { // app will enter in foreground // this method is called on first launch when app was closed / killed and every time app is reopened or change status from background to foreground (ex. mobile call) } func applicationDidBecomeActive(application: UIApplication) { // app becomes active // this method is called on first launch when app was closed / killed and every time app is reopened or change status from background to foreground (ex. mobile call) } } 

Update

As Paulw11 suggests, consider using applicationWillEnterForeground instead of applicationDidBecomeActive

+10
source

There is didFinishLaunchingWithOptions and applicationWillEnterForeground in your AppDelegate.swift.

applicationWillEnterForeground is similar to viewWillAppear for your viewControllers unlike your application, and didFinishLaunchingWithOptions is similar to viewDidLoad for your application. For more information check out UIApplicationDelegate

+5
source

In ViewController.swift in viewDidLoad() write any of them accordingly:

 NotificationCenter.default.addObserver(self, selector: #selector(self.update), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(self.update), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil) 

(EDIT: update for Swift 3)

(EDIT: removed the part of this message that was incorrect. IOS automatically triggers these notifications to any added observer, no need to add code to call them in the App Delegate)

+3
source

Use UIApplicationDidBecomeActive notification.

+2
source

When the application is completed and the user clicks on the application icon, so the first method is called

 didFinishLaunchingWithOptions 

and when the application is in the background and comes to the fore

 applicationWillEnterForeground 

therefore, you can call Update () in both methods. But when the method called didFinishLaunchingWithOptions does not call in applicationWillEnterForeground. Since both methods are launched when the application starts, and when the application enters the foreground mode, only the call to the WillEnterForeground method is called. This can be controlled by the bool flag.

0
source

All Articles