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

查看:137
本文介绍了按下主页按钮时检测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 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?

推荐答案

这些是你的选择

在你的app delegate中:

In your 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:.
}

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

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