Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function every time user opens application

Tags:

ios

swift

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()     
    }
like image 542
Joe Avatar asked Jul 21 '16 07:07

Joe


6 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

like image 65
lubilis Avatar answered Nov 14 '22 06:11

lubilis


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)

like image 44
VRAwesome Avatar answered Nov 14 '22 06:11

VRAwesome


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

like image 7
Happiehappie Avatar answered Nov 14 '22 04:11

Happiehappie


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() {

    }

like image 4
Bijender Singh Shekhawat Avatar answered Nov 14 '22 05:11

Bijender Singh Shekhawat


Use UIApplicationDidBecomeActive notification.

like image 2
Juri Noga Avatar answered Nov 14 '22 05:11

Juri Noga


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

like image 1
Rakesh Mandloi Avatar answered Nov 14 '22 04:11

Rakesh Mandloi