NSDictionary and NSUserNotification Question

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

  1. macrumors 6502a

    DavidBlack

    Joined:
    Jan 27, 2013
    Location:
    Somewhere In Apple's HQ ;)
    #1
    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:
    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 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    What is calling this method?
     
  3. DavidBlack, Aug 13, 2013
    Last edited: Aug 13, 2013

    thread starter macrumors 6502a

    DavidBlack

    Joined:
    Jan 27, 2013
    Location:
    Somewhere In Apple's HQ ;)
    #3
    Code:
      
                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
    Code:
    - (void)userdefaultsKey1or2Changed:(NSString *)keyPath ofObject:(id)target change:(NSDictionary *)change userInfo:(id)userInfo {}
    It just detects changes in the NSUserDefaults
     
  4. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    What is the value of myInt2? It looks to me like you're generating notifications every myInt2 seconds.
     
  5. thread starter macrumors 6502a

    DavidBlack

    Joined:
    Jan 27, 2013
    Location:
    Somewhere In Apple's HQ ;)
    #5
    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.
    Code:
        NSLog(@"string value is %ld",(long)myInt2);
    
     
  6. macrumors member

    Joined:
    Aug 14, 2011
    #6
    For starters, use notifications with the object reference.. i.e.

    Code:
            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:

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

    Code:
    - (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 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    He's using NSUserNotificationCenter, a separate and very different class from NSNotificationCenter that was introduced in Mountain Lion.
     
  8. macrumors member

    Joined:
    Aug 14, 2011
    #8
    the interval of NSTimer is in seconds...

    So basically:

    Code:
    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
    Code:
    repeats:YES
    . If you just want one instance of the timer being fired after 60 minutes then have:

    Code:
                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