ios 每次用户打开应用程序时如何调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38497575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a function every time user opens application
提问by Joe
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.
每次用户点击我的应用程序图标并打开它时,我都想调用名为 update 的函数。我该怎么做?无论是从后台终止还是用户按下主页按钮,只要应用程序启动,我就可以覆盖哪个方法会触发。
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
更新:我在我的 App Delegate 中实现
func applicationWillEnterForeground(application: UIApplication) {
ViewController().update()
}
回答by lubilis
You need to implement your logic in AppDelegate
您需要在 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
正如 Paulw11 建议的,考虑使用 applicationWillEnterForeground 而不是 applicationDidBecomeActive
回答by VRAwesome
In ViewController.swift
in viewDidLoad()
write either of these in respective manner:
在ViewController.swift
在viewDidLoad()
任一这些在相应的方式写:
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)
(编辑: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)
(编辑:删除了这篇文章中不正确的部分。iOS 会自动向任何添加的观察者触发这些通知,无需在 App Delegate 中添加代码来触发它们)
回答by Happiehappie
In your AppDelegate.swift, there are didFinishLaunchingWithOptions
and applicationWillEnterForeground
.
在您的 AppDelegate.swift 中,有didFinishLaunchingWithOptions
和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
applicationWillEnterForeground
类似于viewWillAppear
您的 viewControllers 而不是您的应用程序,而didFinishLaunchingWithOptions
类似于viewDidLoad
您的应用程序。有关更多信息,请查看UIApplicationDelegate
回答by Juri Noga
Use UIApplicationDidBecomeActive
notification.
使用UIApplicationDidBecomeActive
通知。
回答by Rakesh Mandloi
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
所以你可以在这两种方法中调用 Update()。但是当 didFinishLaunchingWithOptions 调用的方法不在 applicationWillEnterForeground 中调用时。因为当应用程序启动时,两个方法都会被调用,而当应用程序进入前台模式时,只有 WillEnterForeground 方法调用。这可以通过 bool 标志管理