检测何时按下主页按钮 iOS [英] Detect when home button is pressed iOS

查看:20
本文介绍了检测何时按下主页按钮 iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 iOS 应用程序,它们都使用相同的端口来侦听网络信标.在主视图上,当打开另一个视图时,我使用 viewWillDisappear 关闭端口,这很好用.然后我注意到如果我在没有打开另一个视图关闭端口的情况下按下主视图控制器的主页按钮,那么端口保持打开状态并且我的其他应用程序不能再侦听该端口.然后我尝试使用 viewWillUnload,但当我按下主页按钮时,它似乎没有被调用.

I have several iOS apps that all use the same port to listen for a network beacon. On the main view I use viewWillDisappear to close the port when another view is opened, which was working great. Then I noticed if I pressed the home button from the main view controller without opening another view to close the port, then the port stays open and non of my other apps can listen on that port any more. I then tried using viewWillUnload, but that doesn't seem to get called when I press the home button.

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

View will unload 永远不会显示在控制台中,这让我相信该方法永远不会被调用.

View will unload is never displayed in the console, which leads me to believe that the method is never getting called.

有没有办法检测何时按下主页按钮,以便我可以关闭端口?

Is there a way to detect when the home button is pressed so I can close my port?

推荐答案

这些是您的选择

在您的应用委托中:

- (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:.
}

这篇关于检测何时按下主页按钮 iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆