如何在iOS中设置闹钟?

我知道这个问题已经问了很多次StackOverflow,但我不能在我的应用程序中设置警报,因为我是iOS的新手? 我正在按照这个教程来设置闹钟:

在iOS中使用UILocalNotification设置提醒。

但是,它似乎并没有为我工作。

我需要每天设置闹钟让我们说每天5.00 PM 。 我不能使用dateselect器来select时间。

  1. 首先在你的xib (或者代码)设置dateselect器模式:时间(默认是date和时间)

  2. 系统假设被解雇者是当前date,时间是用户select的时间。 这不是一个问题,因为你设置了一个重复的时间间隔,所以它会起作用。 我已经testing过了。

     UILocalNotification *localNotif = [[UILocalNotification alloc] init]; [localNotif setFireDate:datePicker.date]; [localNotif setRepeatInterval:NSDayCalendarUnit]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

PS:这是一个好主意,使用NSDateComponents类将秒设置为0 ,以设置闹钟在您想要的一分钟的第一秒响。 你可以检查:

iOS中的本地通知。

教你如何做到这一点。

 + (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID 

用参数调用这个方法并使用它

  + (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID { NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; //set the notification date/time NSDateComponents *dateComps = [[NSDateComponents alloc] init]; [dateComps setDay:day]; [dateComps setMonth:month]; [dateComps setYear:year]; [dateComps setHour:hours]; [dateComps setMinute:minutes]; [dateComps setSecond:seconds]; NSDate *notificationDate = [calendar dateFromComponents:dateComps]; [dateComps release]; UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = notificationDate; localNotif.timeZone = [NSTimeZone defaultTimeZone]; // Set notification message localNotif.alertBody = alertBody; // Title for the action button localNotif.alertAction = actionButtonTitle; localNotif.soundName = (alertSoundName == nil) ? UILocalNotificationDefaultSoundName : alertSoundName; //use custom sound name or default one - look here to find out more: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html%23//apple_ref/doc/uid/TP40008194-CH103-SW13 localNotif.applicationIconBadgeNumber += 1; //increases the icon badge number // Custom data - we're using them to identify the notification. comes in handy, in case we want to delete a specific one later NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID]; localNotif.userInfo = infoDict; // Schedule the notification [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; [localNotif release]; } 

您可能需要更改dateselect器的样式,以允许除了date之外更改时间。

你可以试试这个

 UILocalNotification *todolistLocalNotification=[[UILocalNotification alloc]init]; [todolistLocalNotification setFireDate:[lodatepicker date]]; [todolistLocalNotification setAlertAction:@"Note list"]; [todolistLocalNotification setTimeZone:[NSTimeZone defaultTimeZone]]; [todolistLocalNotification setAlertBody:text_todolist]; [todolistLocalNotification setHasAction:YES];