如何在iOS中设置闹钟?

问题描述:

我知道这个问题已经问了很多次StackOverflow但我无法在我的应用程序中设置闹钟,因为我对iOS非常陌生?我下面这个教程设置闹钟:如何在iOS中设置闹钟?

Setting a reminder using UILocalNotification in iOS.

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

我需要每天设置闹钟让我们说每天5.00 PM。我不能使用日期选择器来选择时间。

  1. 首先您xib,(或者代码)设定的日期选择器模式:时间(默认为日期&时间)

  2. 系统假定firedate是当前日期和时间用户选择的时间。这不是问题,因为您设置了重复间隔,因此它可以工作。我已经测试过它。

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

PS:这将是使用NSDateComponents类秒设置为0,以便设置报警,在您需要的分钟的第一秒响个好主意。您可以检查:你张贴在如何做到这一点

Local notifications in iOS.

教程。

+0

感谢您的答案,但我设法做不同的方法..它的工作。一个问题..是否有可能弹出自定义通知视图,而不是默认的,也想添加自定义声音,在我的项目中添加可能.. +1为您的支持 – GoCrazy 2012-08-07 18:48:28

+2

您无法显示自定义通知弹出窗口。您可以添加自定义声音,但它应该位于应用程序包中。因此,播放应用程序从互联网上下载的声音是不可能的。您只能在编译之前播放系统声音或您已导入应用程序的声音。不客气。如果您认为它是正确的,请接受答案,以帮助未来的用户找到解决此问题的方法。 – 2012-08-07 20:08:21

+0

是的。多数民众赞成在我所寻找的可以发布一些代码播放应用程序包中的声音,编译前请导入 – GoCrazy 2012-08-07 20:30:46

您可能需要更改日期选择器的样式,以允许更改除日期以外的时间。

+0

感谢您answer..Is可以张贴任何教程设置报警 – GoCrazy 2012-08-07 16:25:33

+1

原始教程期望你已经有了这种环境的一些经验,并且有充分的理由:在任何一个关于这个级别或更高级的教程中,都有许多基本的东西可以跳过。我建议你根据基本知识进行研究,并按照自己的方式完成这些任务,否则你将无法完成任何工作,根据自己的喜好进行更改,或者在出现问题时进行修复。 – Jesper 2012-08-07 16:37:40

+0

感谢您的评论..实际上警报得到通知,但我无法得到弹出窗口..会出现什么问题 – GoCrazy 2012-08-07 16:57:12

+ (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]; 
} 

你可以试试这个

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