Detect when home button is pressed iOS

前端 未结 6 739
半阙折子戏
半阙折子戏 2020-12-05 10:28

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, w

6条回答
  •  醉话见心
    2020-12-05 10:37

    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];
    }
    

提交回复
热议问题