Joe
Joe

Reputation: 250

How to call a function every time user opens application

I want to call the function named update every time user taps my app's icon and open it. How would I do that? Which method fires that I can override whenever the app launches no matter is killed from background or user pressed 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()     
    }

Upvotes: 16

Views: 27790

Answers (6)

Bijender Singh Shekhawat
Bijender Singh Shekhawat

Reputation: 4514

For Swift 5 use this code

  1. Add this line in func viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(self.updateView), name: UIApplication.didBecomeActiveNotification, object: nil)

  2. Add This function

    @objc func updateView() {

    }

Upvotes: 4

VRAwesome
VRAwesome

Reputation: 4803

In ViewController.swift in viewDidLoad() write either of these in respective manner:

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 part of this post that was incorrect. iOS triggers these notifications automatically to any added observer, no need to add code to trigger them in the App Delegate)

Upvotes: 9

lubilis
lubilis

Reputation: 4170

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

Upvotes: 16

Happiehappie
Happiehappie

Reputation: 1074

In your AppDelegate.swift, there are didFinishLaunchingWithOptions and applicationWillEnterForeground.

applicationWillEnterForeground is similar to viewWillAppear for your viewControllers as opposed to your app, while didFinishLaunchingWithOptions is similar to viewDidLoad for your app. For more information, check UIApplicationDelegate

Upvotes: 7

Rakesh Mandloi
Rakesh Mandloi

Reputation: 361

When app in terminated stated and user tap on app icon so first method called is

didFinishLaunchingWithOptions 

and when app in background mode and come to foreground

applicationWillEnterForeground

so u can call Update() in both method.But when Method called by didFinishLaunchingWithOptions don't call in applicationWillEnterForeground.Because when app launch both methods are called and when app enter in foreground mode only WillEnterForeground method call. This can be manage by bool flag

Upvotes: 1

Juri Noga
Juri Noga

Reputation: 4391

Use UIApplicationDidBecomeActive notification.

Upvotes: 2

Related Questions