Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在应用程序终止后,使用NSNotification在UIViewController中处理UILocalNotification_Ios_Uiviewcontroller_Uilocalnotification_Nsnotificationcenter_Nsnotifications - Fatal编程技术网

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
并再次运行应用程序时。在my
UIViewController的
viewDidLoad
之前调用
didReceiveLocalNotification:
方法。然后在
PostNotificationName
之后创建观察者,然后观察者不接收任何内容

我想知道是否有一些最佳实践或模式来处理此类问题

我知道,
didfishlaunchingwithoptions
方法是在
didlocalNotificationReceived
之前调用的,因此可能需要做些什么

更新:

我还发现当is应用程序终止时。点击通知后,它会打开应用程序,调用函数
didfishlaunchingwithoptions
,但绝不调用
didReceiveLocalNotification
。所以我想我将以不同的方式处理这两种情况。

好的,我找到了答案

实际上,我需要手动初始化我的故事板,并且在发布
NSNotification

My
DidFinishLaunchwithOptions:
方法在My
appDelegate
中看起来像:

- (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属性)。看看能不能帮上忙。这让我很累!!谢谢,你节省了我的时间!!:)