Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
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?
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
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?

What is calling this method?
 

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
What is calling this method?

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
 
Last edited:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
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

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

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
What is the value of myInt2? It looks to me like you're generating notifications every myInt2 seconds.

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);
 

heyadrian

macrumors member
Aug 14, 2011
83
0
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"];
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
He's using NSUserNotificationCenter, a separate and very different class from NSNotificationCenter that was introduced in Mountain Lion.
 

heyadrian

macrumors member
Aug 14, 2011
83
0
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);

the interval of NSTimer is in seconds...

NSTimeInterval
Used to specify a time interval, in seconds.

typedef double NSTimeInterval;
Discussion
NSTimeInterval is always specified in seconds; it yields sub-millisecond precision over a range of 10,000 years.

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]];


----------

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

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.