关闭通知中心时调用函数
Call function when Notification Center is dismissed
我正在使用今日小部件(使用 Swift)编写我的第一个 iOS 应用程序。我想知道是否有一个函数在我的应用程序关闭通知中心后返回前台时调用。
我知道我可以使用 Observer 来检查 UIApplicationWillEnterForegroundNotification
,但是在使用我的应用程序时拉下通知中心并再次关闭它时,我的函数不会被调用。
我的问题很简单:
用户不太可能拉下通知中心来操纵我在应用程序中使用的数据,但我仍然必须考虑如果他们这样做会发生什么。用户应该能够通过按今日小部件按钮来保存他的当前位置。
如果在使用我的应用程序时发生这种情况,应用程序将不会检查新数据。
我使用以下代码来确定通知中心是否在应用程序的 运行 时间内打开:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
BOOL notificationCenterCurrentlyDisplayed;
}
- (void) viewDidLoad
{
[super viewDidLoad];
notificationCenterCurrentlyDisplayed = false;
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(onNotificationCenterDisplayed) name:UIApplicationWillResignActiveNotification object:nil];
[defaultCenter addObserver:self selector:@selector(onNotificationCenterDismissed) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void) onNotificationCenterDisplayed
{
notificationCenterCurrentlyDisplayed = true;
NSLog(@"Notification center has been displayed!");
}
- (void) onNotificationCenterDismissed
{
// Reason for this check is because once the app is launched the UIApplucationDidBecomeActiveNotification is called.
if (notificationCenterCurrentlyDisplayed)
{
notificationCenterCurrentlyDisplayed = false;
NSLog(@"Notification center has been dismissed!");
}
}
@end
另外,当用户决定将应用程序关闭到后台时,也会调用显示通知中心的方法。
我正在使用今日小部件(使用 Swift)编写我的第一个 iOS 应用程序。我想知道是否有一个函数在我的应用程序关闭通知中心后返回前台时调用。
我知道我可以使用 Observer 来检查 UIApplicationWillEnterForegroundNotification
,但是在使用我的应用程序时拉下通知中心并再次关闭它时,我的函数不会被调用。
我的问题很简单: 用户不太可能拉下通知中心来操纵我在应用程序中使用的数据,但我仍然必须考虑如果他们这样做会发生什么。用户应该能够通过按今日小部件按钮来保存他的当前位置。
如果在使用我的应用程序时发生这种情况,应用程序将不会检查新数据。
我使用以下代码来确定通知中心是否在应用程序的 运行 时间内打开:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
BOOL notificationCenterCurrentlyDisplayed;
}
- (void) viewDidLoad
{
[super viewDidLoad];
notificationCenterCurrentlyDisplayed = false;
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(onNotificationCenterDisplayed) name:UIApplicationWillResignActiveNotification object:nil];
[defaultCenter addObserver:self selector:@selector(onNotificationCenterDismissed) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void) onNotificationCenterDisplayed
{
notificationCenterCurrentlyDisplayed = true;
NSLog(@"Notification center has been displayed!");
}
- (void) onNotificationCenterDismissed
{
// Reason for this check is because once the app is launched the UIApplucationDidBecomeActiveNotification is called.
if (notificationCenterCurrentlyDisplayed)
{
notificationCenterCurrentlyDisplayed = false;
NSLog(@"Notification center has been dismissed!");
}
}
@end
另外,当用户决定将应用程序关闭到后台时,也会调用显示通知中心的方法。