Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when home button is pressed iOS

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?

like image 919
nick Avatar asked Apr 25 '12 22:04

nick


3 Answers

These are your options

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:.
}
like image 184
Mick MacCallum Avatar answered Oct 07 '22 21:10

Mick MacCallum


The easiest way to handle this is to register to receive the UIApplicationWillResignActiveNotification notification in your view controller.

The event is issued upon a home button press, lock and upon a phone call

- (void) applicationWillResign{
    NSLog(@"About to lose focus");
}

- (void) myVcInitMethod { 
    [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(applicationWillResign)
        name:UIApplicationWillResignActiveNotification 
        object:nil];
}
like image 28
PeterPurple Avatar answered Oct 07 '22 23:10

PeterPurple


In case of Swift User

you can write it like this

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!")
}

also, Don't forget to close it when your app is terminate

deinit {

    // code here...

    NSNotificationCenter.defaultCenter().removeObserver(self)
}
like image 33
Sruit A.Suk Avatar answered Oct 07 '22 23:10

Sruit A.Suk