1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

NSDictionary and NSUserNotification Question

Discussion in 'Mac Programming' started by DavidBlack, Aug 13, 2013.

  1. macrumors 6502a


    Hi, guys I am almost done building this app I am making but I have this problem, I looked online on how to get a random keys and values from a NSDictionary it works fine also I have NSUserNotification to get the values from the NSDictionary and give the user a text/quotation. Here is my code:
    -(void)friendshipTimer:(NSTimer *)friendshipTimer {
        NSLog(@"NSUserNotificationCenter fired");
        // refer to this for more help :http://www.renssies.nl/2012/02/mountain-lion-the-new-notifications-center/
        NSBundle* bundle = [NSBundle mainBundle];
        NSString* plistPath = [bundle pathForResource:@"friendshipKeys" ofType:@"plist"];
        NSDictionary* plisttext = [NSDictionary dictionaryWithContentsOfFile:plistPath];
        //Now to generate and display random sentences, you would need to keep a track of all the keys:
        NSArray* keys = [plisttext allValues];
        //Then select a random key using the index:
        int randomIndex = arc4random() % (keys.count);
        NSString* key = [keys objectAtIndex:randomIndex];
        //Initalize new notification
        NSUserNotification *notification = [[NSUserNotification alloc] init];
        //Set the title of the notification
        [notification setTitle:@"My Title"];
        //Set the text of the notification
        [notification setInformativeText:[NSString stringWithFormat:@"%@",key]];
        //Set the sound, this can be either nil for no sound, NSUserNotificationDefaultSoundName for the default sound (tri-tone) and a string of a .caf file that is in the bundle (filname and extension)
        [notification setSoundName:NSUserNotificationDefaultSoundName];
        //Get the default notification center
        NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
        //Scheldule our NSUserNotification
        [center scheduleNotification:notification];
    But when the notification comes up more that one text/quotation comes up at the same time and overlap each other. Can anyone help?
  2. macrumors 603


    What is calling this method?
  3. DavidBlack, Aug 13, 2013
    Last edited: Aug 13, 2013

    macrumors 6502a


                NSDate *d = [NSDate dateWithTimeIntervalSinceNow:myInt2];
                timerFriendTimer = [[NSTimer alloc] initWithFireDate: d
                                                      interval: myInt2
                                                        target: self
                                                      selector:@selector (friendshipTimer:)
                                                    userInfo:nil repeats:YES];
    It's a timer in
    - (void)userdefaultsKey1or2Changed:(NSString *)keyPath ofObject:(id)target change:(NSDictionary *)change userInfo:(id)userInfo {}
    It just detects changes in the NSUserDefaults
  4. macrumors 603


    What is the value of myInt2? It looks to me like you're generating notifications every myInt2 seconds.
  5. macrumors 6502a


    My myInt2 is an NSInteger. I am just taking the value of the textbox and getting it's integerValue example if I place the number on in the text box. And then multiply it by 60 convert it to minutes.

    myInt2 = [stringTextBoxTabSuccess integerValue] * 60;

    I also have a NSLog to know that is the value just to make sure.
        NSLog(@"string value is %ld",(long)myInt2);
  6. macrumors member

    For starters, use notifications with the object reference.. i.e.

            NSDictionary *notificationInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"item1", @"keyForItem1", object2, @"keyForObject2", [myObject ReturnValue], "Object3", nil;
            [[NSNotificationCenter defaultCenter]postNotificationName:@"myNotification" object:nil userInfo:notificationInfo];
    This way an ENTIRE dictionary object gets passed to the notification centre with all bits inside it. SO on the other end... You would:

    have an observer added with something like:

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:@"somethingHappened" object:nil];
    and in doSomething:

    - (void)doSomething:(NSNotification *)notification{
        NSDictionary *notificationInfo;
        NSObject *myObject;
    notificationInfo = [[notification userInfo] objectForKey:@"somethingHappened"]; // <-- that is the dictionary on its way and it telling which notification it was
        myObject = [notificationInfo objectForKey:@"whateverItWasThatWasSentHere"];
  7. macrumors 603


    He's using NSUserNotificationCenter, a separate and very different class from NSNotificationCenter that was introduced in Mountain Lion.
  8. macrumors member

    the interval of NSTimer is in seconds...

    So basically:

    myInt2 = [stringTextBoxTabSuccess integerValue] * 3600;
    denotes an hour. But also note that the timer will fire every hour after it first fires as you have the
    . If you just want one instance of the timer being fired after 60 minutes then have:

                timerFriendTimer = [[NSTimer alloc] initWithFireDate: d
                                                      interval: myInt2
                                                        target: self
                                                      selector:@selector (friendshipTimer:)
                                                    userInfo:nil repeats:[B][COLOR="Red"]NO[/COLOR][/B]];


    good point :D My bad xD I meant NSNotificationCenter lol xD Joys of getting mixed with stuff I'm doing to get my magic mouse to be less annoying :D

Share This Page