How to access applicationDidEnterBackground from viewController

心不动则不痛 提交于 2019-12-23 14:24:17

问题


How can I call applicationDidEnterBackground that in AppDelegate from a viewController?

I want to run a function in the background of the app without pressing home button.


回答1:


You are not supposed to call that function from anywhere. It is there to let you know when the app enters the background.

Do you simply want to know when the app enters the background? If so, then you can create a notification to help you with that:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnteredBackground:) 
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];



回答2:


Swift 4

NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { (notification) in
}



回答3:


This is a delegate method. You should not call this method manually. If you want to call the code return in this method. You should create a function and move the code there. Then you should call this function.




回答4:


This is another way and it'll run on background also Generally, you register the notification first, in your viewController.m init method (or somewhere else appropriate):

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(someMethod:) 
                                             name: @"NotificationNameHere"
                                           object: nil];

In your App Delegate, fire the notification:

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    [[NSNotificationCenter defaultCenter] postNotification: @"NotificationNameHere"];

}



回答5:


You should register a notification in its viewDidLaod method.

'UIApplicationDidBecomeActiveNotification' will automatically notify your application and given controller, if they have registered for.

 [[NSNotificationCenter defaultCenter]addObserver:self
                                         selector:@selector(yourMethod)
                                             name:UIApplicationDidBecomeActiveNotification
                                           object:nil];



回答6:


In your ViewController

#import "AppDelegate.h"

@property AppDelegate *appDelegate;

In your viewDidLoad

appDelegate=[[UIApplication sharedApplication] delegate];

To call the functions from AppDelegate

 [appDelegate applicationDidEnterBackground:[UIApplication sharedApplication]];



回答7:


In your ViewController

import "AppDelegate.h"

@property AppDelegate *appDelegate; In your viewDidLoad

_appDelegate=[[UIApplication sharedApplication] delegate]; To call the functions from AppDelegate

[_appDelegate applicationDidEnterBackground:[UIApplication sharedApplication]];




回答8:


Swift 4

To run something in your view controller when the app goes into background (typically you would save some user data):

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { (notification) in
        print("app did enter background")

        // run your code here (or whatever)
    }

    // ...

}



回答9:


[[UIApplication sharedApplication].delegate applicationDidEnterBackground: [UIApplication sharedApplication]]


来源:https://stackoverflow.com/questions/24675907/how-to-access-applicationdidenterbackground-from-viewcontroller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!