Ios 在应用程序终止后,使用NSNotification在UIViewController中处理UILocalNotification
我正在使用Ios 在应用程序终止后,使用NSNotification在UIViewController中处理UILocalNotification,ios,uiviewcontroller,uilocalnotification,nsnotificationcenter,nsnotifications,Ios,Uiviewcontroller,Uilocalnotification,Nsnotificationcenter,Nsnotifications,我正在使用UILocalNotification,我想通知我的一个控制器,即使应用程序已终止,也已收到通知 在我的appDelegate中,我实现了以下功能: -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if ([application applicationState] == UIApplicationS
UILocalNotification
,我想通知我的一个控制器,即使应用程序已终止,也已收到通知
在我的appDelegate中,我实现了以下功能:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([application applicationState] == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:notification.userInfo];
}
}
在我的UIViewController
中,我在viewDidLoad
方法上实现了观察器
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didlocalNotificationReceived:) name:@"localNotificationReceived" object:nil];
}
当应用程序在后台运行时,它可以完美地工作。因为已经调用了viewDidLoad
方法,并且观察者正在等待
问题是当我关闭应用程序时。然后,我的控制器的观察者消失了,并且从未调用didlocalNotificationReceived
方法
我想这是因为当我收到localNotification
并再次运行应用程序时。在myUIViewController的viewDidLoad
之前调用didReceiveLocalNotification:
方法。然后在PostNotificationName
之后创建观察者,然后观察者不接收任何内容
我想知道是否有一些最佳实践或模式来处理此类问题
我知道,didfishlaunchingwithoptions
方法是在didlocalNotificationReceived
之前调用的,因此可能需要做些什么
更新:
我还发现当is应用程序终止时。点击通知后,它会打开应用程序,调用函数didfishlaunchingwithoptions
,但绝不调用didReceiveLocalNotification
。所以我想我将以不同的方式处理这两种情况。好的,我找到了答案
实际上,我需要手动初始化我的故事板,并且在发布NSNotification
MyDidFinishLaunchwithOptions:
方法在MyappDelegate
中看起来像:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UIViewController *vc =[storyboard instantiateInitialViewController]; //call the initWithCoder: method of my controller
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
[[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:localNotification.userInfo];
}
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
然后在我的UIViewController
中,我在initWithCoder:
方法中创建NSNotification
观察者,而不是在viewDidLoad:
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didlocalNotificationReceived:) name:@"localNotificationReceived" object:nil];
}
return self;
}
- (void)didlocalNotificationReceived:(NSNotification *)notification
{
//Execute whatever method when received local notification
}
当应用程序未被终止时,我仍然使用didReceiveLocalNotification:
方法:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([application applicationState] == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:notification.userInfo];
}
}
我不确定这是否是最好的做法。但它工作得很好
希望有帮助:)尝试在didDinishLaunching中强制加载视图(通过访问view属性)。看看能不能帮上忙。这让我很累!!谢谢,你节省了我的时间!!:)