Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8335753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:45:59+00:00 2026-06-09T03:45:59+00:00

I am developing a system to keep track of achievements and for that I

  • 0

I am developing a system to keep track of achievements and for that I use NSNotificationCenter. When an achievement is unlocked in a object in the app a notification is sent to JMAchievementHandler which sets a string to YES and saves the progress in NSUserDefaults. My problem is that the notifications are not working probably. Here is my code in JMAchievementHandler:

- (id)init {
    self = [super init];
    if (self) {

        //Set strings to NO
        achievementOne = @"NO";
        achievementTwo = @"NO";
        achievementThree = @"NO";
        achievementFour = @"NO";

        //Add observers
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationWithName:) name:@"achievement1" object:nil];

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

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

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

        //Add observer to observe delegate methods
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationFromDelegate:) name:@"notificationLaunch" object:nil];

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

    }
    return self;
}

- (void)receiveNotificationWithName:(NSNotification *)notification {
    if ([[notification name] isEqualToString:@"achievement1"] || [achievementOne isEqualToString:@"NO"]) {

        //unlock achievement
        NSLog(@"%@ is unlocked", [notification name]);
        achievementOne = @"YES";
    }

    else if ([[notification name] isEqualToString:@"achievement2"] || [achievementTwo isEqualToString:@"NO"]) {

        //unlock achievement
        NSLog(@"%@ is unlocked", [notification name]);
        achievementTwo = @"YES";
    }

    else if ([[notification name] isEqualToString:@"achievement3"] || [achievementThree isEqualToString:@"NO"]) {

        //unlock achievement
        NSLog(@"%@ is unlocked", [notification name]);
        achievementThree = @"YES";
    }

    else if ([[notification name] isEqualToString:@"achievement4"] || [achievementFour isEqualToString:@"NO"]) {

        //unlock achievement
        NSLog(@"%@ is unlocked", [notification name]);
        achievementFour = @"YES";
    }
}

- (void)receiveNotificationFromDelegate:(NSNotification *)notificationDelegate
{
    if ([[notificationDelegate name] isEqualToString:@"notificationLaunch"]) {

        [self loadDataOnLaunch];
    }
    else if ([[notificationDelegate name] isEqualToString:@"notificationEnterBackground"]) {
        [self saveDataOnExit];
    }
}

- (void)loadDataOnLaunch
{
    NSLog(@"loadDataOnLaunch");
}

- (void)saveDataOnExit
{
    NSLog(@"saveDataOnExit");
}

When I try to post a notification the NSLogs are not called. I use the following code to send notifications from my AppDelegate and ViewController.

- (void)achievement1ButtonPressed:(id)sender {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"achievement1" object:self userInfo:nil];
}

I hope you guys can help me out. Thanks a lot

  • Jonas
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:46:01+00:00Added an answer on June 9, 2026 at 3:46 am

    Do you even have a method named receiveNotificationWithName? When you entered the code, the autocomplete should have barfed on that, offering receiveNotificationWithName: instead (unless you wrote it first, without NSNotification*, then added it later…

    Anyway, there is a big difference between the two. Also, your handler is buggy. You should revisit that code.

    There are lots of reasons to prefer blocks, and this points to one of them. You can just put your code right in the registration, and not worry about giving the wrong selector.

    [[NSNotificationCenter defaultCenter] addObserverForName:@"achievement1"
                                                      object:nil
                                                       queue:nil
                                                  usingBlock:^{
        // Handle achievement1 right in this little block.
    }];
    

    These look like one-shot notifications, so if you want to unregister the notification handler after it runs one time, do this (note the __block).

    __block id achievement1 = [[NSNotificationCenter defaultCenter]
                                  addObserverForName:@"achievement1 ":^{
                                              object:nil
                                               queue:nil
                                          usingBlock:^{
        // Handle achievement1 right in this little block.
    
        // Remove observer from notification center
        [[NSNotificationCenter defaultCenter] removeObserver:achievement1];
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a system in Ruby that is going to make use of RabbitMQ
I am developing a system that allows userwritten javascript in widgets. To keep things
I am developing a SYSTEM app for android to manage user accounts on the
I am developing a system which include a server app & a client app,
I'm developing a system, and I have build a code generator that emits a
I am developing a system that will provide many services, say, S1 , S2
I'm developing a Remoting classes library so that I can keep database interaction and
I am developing a simple intranet suggestion box system that lets the employees being
I am developing a system that has folders, groups and permissions. Permissions determine what
I am developing a simple rating system for my employer's profile based web-app. Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.