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()
}
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
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)
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
For Swift 5 use this code
Add this line in func viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.updateView), name: UIApplication.didBecomeActiveNotification, object: nil)
Add This function
@objc func updateView() {
}
Use UIApplicationDidBecomeActive
notification.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With