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

これを処理する最も簡単な方法は、View Controller で 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:.
}

または、View Controller でコードを使用する場合:

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

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

使用する方が適切で、「上部の四角形のキャッチ アンド リリース イベント」をキャッチするためですUIApplicationWillResignActiveUIApplicationDidBecomeActiveこのルート クラスを使用することをお勧めします。

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 に答える