当主页button被按下时检测iOS

我有几个iOS应用程序都使用相同的端口来侦听networking信标。 在主视图中,我使用viewWillDisappear在另一个视图打开时closures端口,这非常有效。 然后我注意到,如果我从主视图控制器按下主页button,而不打开另一个视图closures端口,那么端口保持打开,我的其他应用程序不能再监听该端口。 然后,我尝试使用viewWillUnload,但似乎并没有被调用时,我按Home键。

-(void)viewWillUnload { //[super viewWillUnload]; NSLog(@"View will unload"); [udpSocket close]; udpSocket = nil; } 

视图将不会显示在控制台,这导致我相信这个方法永远不会被调用。

有没有办法检测何时按下主页button,所以我可以closures我的端口?

这些是你的select

在您的应用程序委托中:

 - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } 

处理这个问题的最简单的方法是注册在您的视图控制器中接收UIApplicationWillResignActiveNotification通知。

事件发生在主页button按下,locking和打电话

 - (void) applicationWillResign{ NSLog(@"About to lose focus"); } - (void) myVcInitMethod { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResign) name:UIApplicationWillResignActiveNotification object:nil]; } 

在Swift用户的情况下

你可以这样写

 override func viewDidLoad() { super.viewDidLoad() // code here... NSNotificationCenter.defaultCenter().addObserver( self, selector: "applicationWillResignActive:", name: UIApplicationWillResignActiveNotification, object: nil) } func applicationWillResignActive(notification: NSNotification) { print("I'm out of focus!") } 

另外,不要忘记当你的应用程序终止时closures它

 deinit { // code here... NSNotificationCenter.defaultCenter().removeObserver(self) } 

viewWillUnload通常不会被调用,除非内存不足。 最好是实现应用程序委托方法 applicationDidEnterBackground:applicationWillTerminate:并在那里完成工作,或向应用程序的部分发送通知,以知道如何处理清理过程。

除了在内存不足的情况下,通常不会调用viewWillUnload。 改用这些:

在您的App Delegate中:

 - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } 

或者,如果您想在View Controller中使用代码:

 - (void)viewDidDisappear:(BOOL)animated { //Put code here } - (void)viewWillDisappear:(BOOL)animated { //Put code here } 

更好地使用UIApplicationWillResignActiveUIApplicationDidBecomeActive因为它们捕捉“顶部矩形捕获和释放事件”。 我会build议使用这个根类:

 class VBase: UIViewController { fileprivate var listenersActivated = false override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) onStart() } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) onStop() removeListeners() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. onStop() removeListeners() } internal func iniListeners() { if (!listenersActivated) { NotificationCenter.default.addObserver(self, selector: #selector(onStop), name: NSNotification.Name.UIApplicationWillResignActive, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(onStart), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil) listenersActivated = true } else { } } internal func removeListeners() { NotificationCenter.default.removeObserver(self) listenersActivated = false } internal func onStop() { } internal func onStart() { iniListeners() } } 

覆盖子内部的onStop()onStart()以捕获所有的视图外观/消失

那是,

 class SomeViewController: VBase { ... override func onStart() { super.onStart() someFunctionToInitialize() } override func onStop() { super.onStop() stopTimer() someFunctionToDesctruction() } }