09-20-2011, 03:37 PM
#1 (permalink )
Registered Member
Join Date: Jun 2011
Posts: 33
Local notification date
hi,
I am creating an app that uses the local notification.
I am to see if they worked, I used a datapicker, but I would make sure that the date is decided by the code and not through the datapicker, so delete it.
here is my code right now:
Code:
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = @"Nuove previsioni Meteomar disponibili ora!";
notif.soundName = @"harp.caf";
//notif.applicationIconBadgeNumber = 1;
notif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
Can you help me with the code?
09-20-2011, 05:08 PM
#2 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
So what's the problem? If it doesn't fire, NSLog the date picker's date to see if it correct:
Code:
NSLog(@"The date from the UIDatePicker is:%@", [datePicker.date description]);
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
09-20-2011, 05:20 PM
#3 (permalink )
Registered Member
Join Date: Jun 2011
Posts: 33
No, you do not understand. I would delete the datapicker and manage the time of the notification code, I tried replacing it with this code, but should not be:
Code:
notif.fireDate = [06:00:00 date];
09-20-2011, 07:30 PM
#4 (permalink )
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Quote:
Originally Posted by
fast
No, you do not understand. I would delete the datapicker and manage the time of the notification code, I tried replacing it with this code, but should not be:
Code:
notif.fireDate = [06:00:00 date];
Of course that doesn't work. It's not legal code.
If you want to create a date from a constant value in your code, convert the date to a double with code like this
Code:
NSDate* pickerDate;
//Get the date from your picker.
NSLog(@"Picker date in seconds is %d",
(int) [pickerDate timeIntervalSinceReferenceDate]);
Write down that value, and then add this code to your program:
Code:
Code:
notif.fireDate =
[NSDate dateWithTimeIntervalSinceReferenceDate: xxx];
[/quote]
Where "xxx" is the number you get in the log statement above.
09-22-2011, 10:35 AM
#5 (permalink )
Registered Member
Join Date: Jun 2011
Posts: 33
and if i write:
Code:
notif.fireDate = [NSDate dateWithTimeIntervalSinceReferenceDate:18];
09-22-2011, 11:19 AM
#6 (permalink )
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Quote:
Originally Posted by
fast
and if i write:
Code:
notif.fireDate = [NSDate dateWithTimeIntervalSinceReferenceDate:18];
Where did you get the number 18?
The method dateWithTimeIntervalSinceReferenceDate takes as input a number of seconds since midnight January 1, 2001. So that call woudl give you a date of 00:00:18 on January 1, 2001.
Somehow, I doubt if that's what you want.
09-22-2011, 11:46 AM
#7 (permalink )
Registered Member
Join Date: Jun 2011
Posts: 33
I would for example set the alert to appear tomorrow night at 7PM.
how do I do?
Code:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setYear:2011];
[dateComps setMonth:9];
[dateComps setDay:22];
[dateComps setHour:19];
[dateComps setMinute:00];
[dateComps setSecond:00];
notif.fireDate = [calendar dateFromComponents:dateComps];
notif.timeZone = [calendar timeZone];
Last edited by fast; 09-22-2011 at 12:07 PM .
09-22-2011, 06:37 PM
#8 (permalink )
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Quote:
Originally Posted by
fast
I would for example set the alert to appear tomorrow night at 7PM.
how do I do?
Code:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setYear:2011];
[dateComps setMonth:9];
[dateComps setDay:22];
[dateComps setHour:19];
[dateComps setMinute:00];
[dateComps setSecond:00];
notif.fireDate = [calendar dateFromComponents:dateComps];
notif.timeZone = [calendar timeZone];
The code you posted should work perfectly.
09-22-2011, 06:39 PM
#9 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
First get the current date using
Code:
NSDate *now = [NSDate date];
Then get the hour using NSCalendar's componentsFromDate method:
Code:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *currentHour = [calendar components:NSHourCalendarUnit fromDate:now];
Then find the difference between the hour and 7 pm
Code:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *currentHour = [calendar components:NSHourCalendarUnit fromDate:now];
int hour = [currentHour hour];
int offset = 19 - hour;
Then add a day and the offset using NSCalendar's dateByAddingComponents method:
Code:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *currentHour = [calendar components:NSHourCalendarUnit fromDate:now];
int hour = [currentHour hour];
int offset = 19 - hour;
NSDateComponents *timeToAdd = [NSDateComponents alloc] init];
[timeToAdd setDay: 1];
[timeToAdd setHour: offset];
NSDate *fireDate = [calendar dateByAddingComponents:timeToAdd toDate:now options:0];
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
09-22-2011, 07:11 PM
#10 (permalink )
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Quote:
Originally Posted by
Domele
First get the current date using
Code:
NSDate *now = [NSDate date];
Then get the hour using NSCalendar's componentsFromDate method:
Code:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *currentHour = [calendar components:NSHourCalendarUnit fromDate:now];
Then find the difference between the hour and 7 pm
Code:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *currentHour = [calendar components:NSHourCalendarUnit fromDate:now];
int hour = [currentHour hour];
int offset = 19 - hour;
Then add a day and the offset using NSCalendar's dateByAddingComponents method:
Code:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *currentHour = [calendar components:NSHourCalendarUnit fromDate:now];
int hour = [currentHour hour];
int offset = 19 - hour;
NSDateComponents *timeToAdd = [NSDateComponents alloc] init];
[timeToAdd setDay: 1];
[timeToAdd setHour: offset];
NSDate *fireDate = [calendar dateByAddingComponents:timeToAdd toDate:now options:0];
Will that work if the current hour is before or after 19:00? I'm too tired to think it through completely.
09-22-2011, 07:16 PM
#11 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
Yes for example if it's 13:00, then the offset will be 6 hours. It'll had one day and 6 hours. If it's 21:00, then it'll add 22 hours (1 day - 2 hours).
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 396
15 members and 381 guests
7twenty7 , eski , EvilElf , fiftysixty , HemiMG , iOS.Lover , jarv , n00b , pbart , Pudding , sacha1996 , Sami Gh , UMAD , VinceYuan , yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28