1

我正在使用UILocalNotification,我想通知我的控制器之一,即使應用程序已終止,也已收到通知。在應用程序終止後使用NSNotification處理UIViewController中的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並再次運行應用程序。在我的UIViewControllerviewDidLoad之前調用didReceiveLocalNotification:方法。然後在PostNotificationName之後創建觀察者,然後觀察者不收到任何東西。

我想知道是否有一些最佳實踐或模式來處理這類問題。

我知道didFinishLaunchingWithOptions方法是在didlocalNotificationReceived之前調用的,所以這裏可能有些事情要做。

更新:

我還發現應用何時終止。一旦你點擊通知,它會打開應用程序,調用功能didFinishLaunchingWithOptions,但從來沒有打電話didReceiveLocalNotification。所以我認爲我會以不同的方式處理這兩種情況。

+0

嘗試在didDinishLaunching中強制加載視圖(通過訪問視圖屬性)。看看是否有幫助。 –

回答

2

好的,我找到了答案。

我其實,手動初始化我的故事板,並保持謹慎,我在我appDelegate張貼NSNotification

didFinishLaunchingWithOptions:方法之前初始化我的主要觀點看起來像這樣:

- (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]; 
    } 
} 

我不知道這是否是最好的做法。但它運作良好!

希望它會幫助:)

+0

其工作我awsome!謝謝,你節省了我的時間! :) –

相關問題