在DatePicker时执行操作

时间:2013-08-16 15:42:43

标签: ios datepicker nsdate uilocalnotification

当当前时间等于UIDatePicker中选择的时间时,我需要发送短信。我怎么能这样做?您不需要包含发送消息的代码,我已经编码了。我已经尝试过各种各样的NSTimer和if - then语句,但都没有。

编辑:自从我写了这个问题以来,我发现了一种更好的做事方式。我只需要设置一个本地通知,并在收到时执行我的代码 - (void)didRevieveLocalNotification。这就是我所拥有的,这样任何一个google都可以得到帮助。

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

1 个答案:

答案 0 :(得分:1)

好吧,我会使用本地通知......类似这样的

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

然后在你的AppDelegate

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

//Code to manage the notification logic


}

希望这会有所帮助,即使在后台,用户也会收到警报..如果在后台,用户必须单击警报,让您的应用程序知道本地通知已触发,如果他确实(或者他已经在您的应用上) ,然后app委托方法将触发让你的应用知道通知已被解雇...

希望这有帮助!