问题描述:

I need to send a text message when the current time equals the time selected in the UIDatePicker. How might I do this? You don't need to include the code to send the message, I already have that coded. I've tried all sorts of things with NSTimer and if - then statements but none have worked.

Edit: Since I wrote this question I've found a better way to do things. I just need to set a local notification and when received execute my code with -(void)didRevieveLocalNotification. Here is what I have so that any googlers can hopefully be helped.

NSDate *pickerDate = [self.datePicker date];

//Set Local Notification

UILocalNotification *notif = [[UILocalNotification alloc] init];

notif.fireDate = pickerDate;

notif.timeZone = [NSTimeZone defaultTimeZone];

//----------------------------------------------------------------------

notif.alertBody = @"Tap to send your text message!";

notif.alertAction = @"send message...";

notif.soundName = @"sms_alert_nova.caf";

notif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:notif];

网友答案:

well i would use a local notification... something like this

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = theDate //The date that your picker has selected

notification.alertBody = @"Hey, the time just expired!"
notification.applicationIconBadgeNumber = 1;

[[UIApplication sharedApplication] scheduleLocalNotification:notif];

Then in your AppDelegate

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

//Code to manage the notification logic


}

Hope this helps, the user will get the alert even if on background.. if on background the user must click the alert to let your application know that the local notification triggered, if he does (or he is on your app already, then the app delegate method will trigger letting your app know that the notification fired...

Hope this helps!

相关阅读:
Top