如何发出行动通知按"主页按钮" [目的-C]

时间:2018-05-25 07:47:07

标签: ios objective-c nsnotificationcenter

当按下HOME按钮并且应用程序关闭时应用程序进入后台时,我需要使用SELECT "brand_id","brand_name","eq_ref_raw","oe","btimestamp" FROM ( select ... ) tab WHERE brand_id >= 5 AND brand_id < 6 NSNotification对类进行签名,每个对象都需要通知应用程序已关闭在控制台中。

所以我找到了问题的答案:

总申请: 在AppDelegate中:

NSNotificationCentre

}

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

UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateInactive) {
    NSLog(@"Sent to background by locking screen");
} else if (state == UIApplicationStateBackground) {
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"kDisplayStatusLocked"]) {
        NSLog(@"Sent to background by home button/switching to other app");
    } else {
        NSLog(@"Sent to background by locking screen");
    }
}

}

但在我的情况下,为了对每个班级使用通知,我需要这样做:

- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateActive) {
    NSLog(@"Open an aplication from background");
}

1 个答案:

答案 0 :(得分:1)

没有专门的方式来监听主页按钮,但是当应用程序进入后台时,您可以从AppDelegate中的applicationDidEnterBackground获得通知,在终止之前,您可以从AppDelegate中的applicationWillTerminate发出通知