28

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

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

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

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

4

6 回答 6

46

这些是您的选择

在您的应用委托中:

- (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:.
}
于 2012-04-25T22:41:41.237 回答
32

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

该事件在按下主页按钮、锁定和打电话时发出

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

- (void) myVcInitMethod { 
    [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(applicationWillResign)
        name:UIApplicationWillResignActiveNotification 
        object:nil];
}
于 2012-11-06T22:26:45.843 回答
13

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

另外,不要忘记在您的应用程序终止时关闭它

deinit {

    // code here...

    NSNotificationCenter.defaultCenter().removeObserver(self)
}
于 2015-10-03T23:04:28.867 回答
5

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

于 2012-04-25T22:43:02.860 回答
5

viewWillUnload 通常不会被调用,除非内存不足。 改用这些:

在您的应用程序委托中:

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

或者,如果您想在视图控制器中使用代码:

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

- (void)viewWillDisappear:(BOOL)animated
{
//Put code here
}
于 2012-04-25T23:36:10.703 回答
5

更好地使用UIApplicationWillResignActive并且UIApplicationDidBecomeActive由于它们捕获“顶部矩形捕获和释放事件”。我建议使用这个根类:

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()
    }
}
于 2016-10-25T14:14:58.560 回答