0

I have creating local notification process using objective C for iPhone application. I want to pass the data after didReceiveLocalNotification to Main view controller.

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {


    if (app.applicationState == UIApplicationStateInactive ) {
        NSLog(@"app not running");

       // I need to pass the data to main view controller 


    }else if(app.applicationState == UIApplicationStateActive )  {
        NSLog(@"app running");


        // I need to pass the data to main view controller

    }

    NSLog(@"Recieved Notification %@",notif);  // Handle the notificaton when the app is running 

}
Ben
  • 73
  • 2
  • 12

1 Answers1

2

you can use NSNotificationCenter to pass data.

in MainViewController.m

in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifMainController:) name:@"notifMainController" object:nil];

-(void)notifMainController:(NSNotification *)notif
{
    NSLog(@"%@",notif.object);
}

in AppDelegate

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif 
{

 NSLog(@"Recieved Notification %@",notif);  // Handle the notificaton when the app is running 

if (app.applicationState == UIApplicationStateInactive ) 
{
    NSLog(@"app not running");
   // I need to pass the data to main view controller 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            MainViewController *obj = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
            [self.navC pushViewController:obj animated:NO];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController" object:@"your string"];
        });

}
else if(app.applicationState == UIApplicationStateActive )  
{
    NSLog(@"app running");
    // I need to pass the data to main view controller
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController" object:@"your string"];
}

}

you can pass string in [[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController" object:@"your string"]; like this in object. Maybe this will help you.

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
  • How can I send the data to third view controller from Appdelegate. I have programmatically created everything in my app code. – Ben Nov 17 '14 at 05:04
  • @Mano i wrote all code. i've added nsnotificationcenter and using NSNotitification you can send data. please read whole answer. – ChintaN -Maddy- Ramani Nov 17 '14 at 05:09
  • @Mano if answer is helpful then upvote or accept is as answer. – ChintaN -Maddy- Ramani Nov 17 '14 at 05:32
  • sure I did vote this....I need another one doubt..I want to pass string value but there notif.object via how can i send..Please send your answer... – Ben Nov 17 '14 at 05:45
  • `[[NSNotificationCenter defaultCenter]postNotificationName:@"notifMainController" object:@"string"]; ` you can send string in object and in `notifMainController:` method you'll get string in `notif.object`. – ChintaN -Maddy- Ramani Nov 17 '14 at 05:47
  • @Mano check out my updated answer. you can send any object when you postnotification with object. – ChintaN -Maddy- Ramani Nov 17 '14 at 05:49
  • 1
    Also: Here is a great article about it: http://nshipster.com/nsnotification-and-nsnotificationcenter/ – Antzi Nov 17 '14 at 05:52
  • wow nice @Antzi..Actually I have received notifcation data into the main view controller but i dont know how to comapre my table data and notification data both are same or not. if both are same mean i need to change the tableview cell color. Do you have any idea about that...? – Ben Nov 17 '14 at 05:59
  • @Mano you can ask another question with your issue regarding that and SO people will help you. but for that you should try your self first and then ask question – ChintaN -Maddy- Ramani Nov 17 '14 at 06:04