Breaking News
Home / iOS / iOS Snippets / How do you post and recieve notifications via Notification Center in iOS

How do you post and recieve notifications via Notification Center in iOS

Snippet

Notification Center allows to send internal asynchronous notification with the application. Ex. you can define these methods as below to send notification when required in your app.

  1. (void) about:(id) sender
  2. {
  3.         [[NSNotificationCenter defaultCenter] postNotificationName:@"about" object:@""];
  4. }
  5.  
  6. (void) highScores:(id) sender
  7. {
  8.         [[NSNotificationCenter defaultCenter] postNotificationName:@"highScores" object:@""];
  9. }

Now these notification need to be received and worked on. So register a method that would recieve these notification at application startup. Then implement this method to get the notification name and take the appropriate action. Normally this registration and the method both are done in the Application Delegate class itself.

  1. // register
  2. (void)applicationDidFinishLaunching:(UIApplication *)application
  3. {    
  4.         NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter];
  5.         [notifyCenter addObserver:self selector:@selector(trackNotifications:) name:nil object:nil];
  6.  
  7. }
  8.  
  9. // method that would be invoked
  10. (void) trackNotifications: (NSNotification *) notification
  11. {
  12.         id nname = [notification name];
  13.        
  14.         if([nname isEqual:@"about"])
  15.         {
  16.           [self loadHtml:@"about" withTitle:@"About"];
  17.         }else if([nname isEqual:@"highScores"])
  18.         {
  19.            HighScoresViewController *high = [[HighScoresViewController alloc] init];
  20.            self.highScoresViewController = high;
  21.            [high release];
  22.  
  23.           [self showUIViewController:highScoresViewController];
  24.                
  25.         }
  26. }

<!>

[pb_builder]

Check Also

EKCalendar – That account does not allow calendars to be added or removed

Usecase When I am trying to create a new Calendar programmatically using the EventKit API …

Advertisment ad adsense adlogger