Showing an alert/notification in Iphone at a particular time(set by either user or developer)
I would like to know what IOS functionality to use to enable me to display a alert/notification at a particular time on the iphone. The time would be set either by the user or the developer using the UIDatePicker. Running the Local Notification Tutorial, the alert/notification is getting displaye开发者_开发知识库d only when the app is in the background(please correct me if I am wrong)
It would be great if someone could help me out with this.
So I think you're on the right track here: not sure what more I can add.
You can use a local notification to display an alert at any given time. As you see from the tutorial, notifications aren't triggered when your app is in the foreground.
But there's nothing to stop you from writing code in your app to alert users at the time the notification is required when your app is in the foreground: and this is probably more ideal. You may want to take advantage of the UI options offered to you when in the foreground to do something different, or you could just display your own UIAlertView.
I can give you my short code to put a specific time on date... I was trying to calculate an specific month time. Example (from 1-1-2011 to 2-1-2011 at 0:0:00 am). Where I live, I have 2h of difference respect of GMT.
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
components = [[NSDateComponents alloc] init];
NSCalendar *gregorian = [NSCalendar currentCalendar];
[components setMonth:1];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
dategre = [gregorian dateByAddingComponents:components toDate:[datePicker date] options:0];
//These steps below are important to put your specific date, in this case specific hour
selected2 = [dateFormatter stringFromDate:dategre];
dategre = [dateFormatter dateFromString:selected2];
//You would see on debugger console something like this: //datePicker.date = 2011-01-01 08:42:16 GMT //dategre = 2011-01-31 22:00:00 GMT (for us, this is equal to 2-1-2011 at 0:00:00 am)
I hope I can help you,
bye,
Willy.
精彩评论