Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 13, 2013, 06:05 PM   #1
DavidBlack
macrumors 6502
 
Join Date: Jan 2013
Location: Somewhere In Apple's HQ ;)
NSDictionary and NSUserNotification Question

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?
__________________
21.5"iMac late 2012, 2.7 GHz, 8 GB RAM, 1TB HD; 11.6" Macbook Air Mid 2011, 1.6 GHz, 2 GB RAM; iPhone 5 White 32GB; iPod Touch 5th gen, White, 32GB
DavidBlack is offline   0 Reply With Quote
Old Aug 13, 2013, 07:29 PM   #2
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by DavidBlack View Post
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?
ArtOfWarfare is offline   0 Reply With Quote
Old Aug 13, 2013, 08:08 PM   #3
DavidBlack
Thread Starter
macrumors 6502
 
Join Date: Jan 2013
Location: Somewhere In Apple's HQ ;)
Quote:
Originally Posted by ArtOfWarfare View Post
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
__________________
21.5"iMac late 2012, 2.7 GHz, 8 GB RAM, 1TB HD; 11.6" Macbook Air Mid 2011, 1.6 GHz, 2 GB RAM; iPhone 5 White 32GB; iPod Touch 5th gen, White, 32GB

Last edited by DavidBlack; Aug 13, 2013 at 08:15 PM.
DavidBlack is offline   0 Reply With Quote
Old Aug 13, 2013, 09:27 PM   #4
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by DavidBlack View Post
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.
ArtOfWarfare is offline   0 Reply With Quote
Old Aug 13, 2013, 10:06 PM   #5
DavidBlack
Thread Starter
macrumors 6502
 
Join Date: Jan 2013
Location: Somewhere In Apple's HQ ;)
Quote:
Originally Posted by ArtOfWarfare View Post
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);
__________________
21.5"iMac late 2012, 2.7 GHz, 8 GB RAM, 1TB HD; 11.6" Macbook Air Mid 2011, 1.6 GHz, 2 GB RAM; iPhone 5 White 32GB; iPod Touch 5th gen, White, 32GB
DavidBlack is offline   0 Reply With Quote
Old Aug 16, 2013, 11:51 AM   #6
heyadrian
macrumors member
 
Join Date: Aug 2011
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"];
heyadrian is offline   0 Reply With Quote
Old Aug 16, 2013, 12:18 PM   #7
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
He's using NSUserNotificationCenter, a separate and very different class from NSNotificationCenter that was introduced in Mountain Lion.
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Including the ones paired with other Macs on your network.
ArtOfWarfare is offline   0 Reply With Quote
Old Aug 19, 2013, 12:43 AM   #8
heyadrian
macrumors member
 
Join Date: Aug 2011
Quote:
Originally Posted by DavidBlack View Post
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...

Quote:
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:NO];


----------

Quote:
Originally Posted by ArtOfWarfare View Post
He's using NSUserNotificationCenter, a separate and very different class from NSNotificationCenter that was introduced in Mountain Lion.
good point 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
heyadrian is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: XML -> NSDictionary DennisBlah iPhone/iPad Programming 5 May 19, 2014 03:51 PM
Resolved: How do I convert an NSData object to an NSDictionary object, and vice versa? moonman239 iPhone/iPad Programming 6 May 2, 2014 01:17 PM
Where do I initialise NSDictionary GerardGrundy iPhone/iPad Programming 9 Sep 8, 2013 10:04 PM
Theoretical Question on NSDictionary / NSArray ArtOfWarfare iPhone/iPad Programming 5 Jul 13, 2013 12:05 PM
JSON and NSDictionary mngoose iPhone/iPad Programming 6 Jun 7, 2012 08:33 AM

Forum Jump

All times are GMT -5. The time now is 03:12 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC