闹钟的话 要入住系统队列的.你可以通过设置本地消息给系统实现闹钟的效果. 时间可以是一次性的 可以是周期性的 具体可以看看文档
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil)
{
NSDate *now=[NSDate new];
notification.fireDate=[now addTimeInterval:20]; //s
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.alertBody=@"hello";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
notification.repeatInterval = NSWeekCalendarUnit;
可以看重复周期,每周,还是每天,还是每月,还是每年。。等等。
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil)
{
NSDate *now=[NSDate new];
//notification.fireDate=[now addTimeInterval:period];
notification.fireDate = [now dateByAddingTimeInterval:period];
NSLog(@"%d",period);
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.soundName = @"ping.caf";
//notification.alertBody=@"TIME!";
notification.alertBody = [NSString stringWithFormat:@"@%时间到了!",nameStr];
NSDictionary* info = [NSDictionary dictionaryWithObject:uniqueCodeStr forKey:CODE];
notification.userInfo = info;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
设置的时间到了以后,会自动在桌面弹出一个提示框,点显示后,就可以启动软件。然后在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
NSLog(@"Recieved Notification %@",localNotif);
NSDictionary* infoDic = localNotif.userInfo;
NSLog(@"userInfo description=%@",[infoDic description]);
NSString* codeStr = [infoDic objectForKey:CODE];
}
}里,对lanchOptions进行处理,找到它里面的信息,就可以拿到设置时的需要处理的东西,就可以继续操作了。
如果此时你的客户端 软件仍在打开,则会调用
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
}
一样的处理方法。
闹钟所实现的基本功能:定时提醒
//定义声音
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();
// Get the URL to the sound file to play
soundFileURLRef = CFBundleCopyResourceURL (
mainBundle,
CFSTR ("tap"),
CFSTR ("aif"),
NULL
);
//将nsstring转为cfstring
// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID
(
soundFileURLRef,
&soundFileObject
);//声音的绑定(类似数据库时用的数据库指针)
利用之前介绍的uidatepicker选取要提醒的时间
//计算多少秒后闹钟响应时间
int hm=(hs*3600)+(ms*60)-sec;
//建立后台消息对象
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil)
{
notification.repeatInterval=NSDayCalendarUnit;
NSDate *now1=[NSDate new];
notification.fireDate=[now1 dateByAddingTimeInterval:hm];//距现在多久后触发代理方法
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.soundName = @"tap.aif";
notification.alertBody = [NSString stringWithFormat:NSLocalizedString(@"你设置的时间是:%i : %i .",nil),htime1 ,mtime1];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[now1 release];
}
点击确定时触发此方法
-(void)notfacation{
//获取当前时间
sure=YES;
NSDate* now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [calendar components:unitFlags fromDate:now];
hour = [comps hour];
min = [comps minute];
sec = [comps second];
htime1=[textField.text intValue];
mtime1=[textField1.text intValue];
hs=htime1-hour;
ms=mtime1-min;
//设置弹出框提醒用户
UIAlertView *at=[[UIAlertView alloc] initWithTitle:@"!"
message:[NSString stringWithFormat:@"你设置的时间: %i:%i ",htime1,mtime1]
delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"关闭",nil];
[at setDelegate:self];
[at show];
[at release];
}
所设定的时间到了会触发此代理
//到时间时触发的代理
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"123123123131231231++++++++++++");
AudioServicesPlaySystemSound (self.soundFileObject);
sure=NO;
UIApplicationState state = application.applicationState;
if (state == UIApplicationStateActive)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"时间提醒"
message:notification.alertBody
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
iOS下的Notification的使用 Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notification种类,本地和远程。本地的Notification由iOS下NotificationManager统一管理,只需要将封装好的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification,应用只需设计好处理Notification的方法就完成了整个Notification流程了。本地Notification所使用的对象是UILocalNotification,UILocalNotification的属性涵盖了所有处理Notification需要的内容。UILocalNotification的属性有fireDate、timeZone、repeatInterval、repeatCalendar、alertBody、 alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、 soundName和userInfo。UILocalNotification的调度其中fireDate、timeZone、repeatInterval和repeatCalendar是用于UILocalNotification的调度。fireDate是UILocalNotification的激发的确切时间。timeZone是UILocalNotification激发时间是否根据时区改变而改变,如果设置为nil的话,那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发。 repeatInterval是UILocalNotification被重复激发之间的时间差,不过时间差是完全根据日历单位(NSCalendarUnit),例如每周激发的单位,NSWeekCalendarUnit,如果不设置的话,将不会重复激发。 repeatCalendar是UILocalNotification重复激发所使用的日历单位需要参考的日历,如果不设置的话,系统默认的日历将被作为参考日历。UILocalNotification的提醒内容alertBody、alertAction、hasAction和alertLaunchImage是当应用不在运行时,系统处理UILocalNotification提醒是需要的内容。alertBody是一串现实提醒内容的字符串(NSString),如果 alertBody未设置的话,Notification被激发时将不现实提醒。alertAction也是一串字符(NSString),alertAction的内容将作为提醒中动作按钮上的文字,如果未设置的话,提醒信息中的动作按钮将显示为“View”相对文字形式。alertLaunchImage是在用户点击提醒框中动作按钮(“View”)时,等待应用加载时显示的图片,这个将替代应用原本设置的加载图片。hasAction是一个控制是否在提醒框中显示动作按钮的布尔值,默认值为YES。UILocalNotification的其他部分applicationIconBadgeNumber、soundName和userInfo将使UILocalNotification更完整。applicationIconBadgeNumber是显示在应用图标右上角的数字,这样让用户直接了解到应用需要处理的 Notification。soundName是另一个UILocalNotification用来提醒用户的手段,在Notification被激发之后将播放这段声音来提醒用户有Notification需要处理(有声音设置时,声音名称要加上扩展名,如sound1.mp3),如果不设soundName的话,Notification被激发是将不会有声音播放,除去应用特制的声音以外,也可以将soundName设为UILocalNotificationDefaultSoundName来使用系统默认提醒声音。userInfo是Notification用来传递数据的NSDictionary。登记UILocalNotification在设置完UILocalNotification对象之后,应用需要在系统Notification处理队列中登记已设置完的UILocalNotification对象。登记UILocalNotification * localNotification的方式为: [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];在有些时候,应用可能需要直接激发一个Notification而不是等一段时间在激发,应用可以以下的方式直接触发已设好的Notification: [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];处理UILocalNotification在提醒框动作按钮被点击后,应用开始运行时,可以在 -(BOOL)application:didFinishLaunchingWithOptions:这个Application delegate方法中处理。可以通过以下方式来加载为最近未处理的Notification: UILocalNotification * localNotif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];如果应用正在运行时,可以通过覆盖在Application Delegate中的方法-(void)application:didReceiveLocalNotification:来处理 Notification。作为方法的第二个参数为UILocalNotification对象,只需处理对象携带的userInfo来处理响应的动作。取消UILocalNotification可以使用以下两个方式来取消一个已经登记的Notification,第一个方式可以直接取消一个指定的Notification,第二个方式将会把该应用已登记的Notification一起取消 [[UIApplication sharedApplication] cancelLocalNotification:localNotification]; [[UIApplication sharedApplication] cancelAllLocalNotification];一般需要应用程序后台运行时才会显示提示,前台运行时一般不显示提示
。如果想要当应用程序前台应行时也显示提示,则可以通过将下面函数加到appDelegate中实现:- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ UIApplicationState state = application.applicationState;// NSLog(@"%@,%d",notification,state); if (state == UIApplicationStateActive) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:notification.alertBody delegate:self cancelButtonTitle:@"Close" otherButtonTitles:@"OK",nil]; [alert show]; [alert release]; }}总结本地Notification的机制在应用开发中非常有效,可以很好的帮助开发者管理一些指定时间需要发生的事件,例如闹钟类的应用。而且因为系统统一对Notification的管理,让同样的任务可以非常简单得被处理,而无需让应用浪费资源去等待事件的触发
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil)
{
NSDate *now=[NSDate new];
notification.fireDate=[now addTimeInterval:20]; //s
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.alertBody=@"hello";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
notification.repeatInterval = NSWeekCalendarUnit;
可以看重复周期,每周,还是每天,还是每月,还是每年。。等等。
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil)
{
NSDate *now=[NSDate new];
//notification.fireDate=[now addTimeInterval:period];
notification.fireDate = [now dateByAddingTimeInterval:period];
NSLog(@"%d",period);
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.soundName = @"ping.caf";
//notification.alertBody=@"TIME!";
notification.alertBody = [NSString stringWithFormat:@"@%时间到了!",nameStr];
NSDictionary* info = [NSDictionary dictionaryWithObject:uniqueCodeStr forKey:CODE];
notification.userInfo = info;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
设置的时间到了以后,会自动在桌面弹出一个提示框,点显示后,就可以启动软件。然后在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
NSLog(@"Recieved Notification %@",localNotif);
NSDictionary* infoDic = localNotif.userInfo;
NSLog(@"userInfo description=%@",[infoDic description]);
NSString* codeStr = [infoDic objectForKey:CODE];
}
}里,对lanchOptions进行处理,找到它里面的信息,就可以拿到设置时的需要处理的东西,就可以继续操作了。
如果此时你的客户端 软件仍在打开,则会调用
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
}
一样的处理方法。
闹钟所实现的基本功能:定时提醒
//定义声音
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();
// Get the URL to the sound file to play
soundFileURLRef = CFBundleCopyResourceURL (
mainBundle,
CFSTR ("tap"),
CFSTR ("aif"),
NULL
);
//将nsstring转为cfstring
// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID
(
soundFileURLRef,
&soundFileObject
);//声音的绑定(类似数据库时用的数据库指针)
利用之前介绍的uidatepicker选取要提醒的时间
//计算多少秒后闹钟响应时间
int hm=(hs*3600)+(ms*60)-sec;
//建立后台消息对象
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil)
{
notification.repeatInterval=NSDayCalendarUnit;
NSDate *now1=[NSDate new];
notification.fireDate=[now1 dateByAddingTimeInterval:hm];//距现在多久后触发代理方法
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.soundName = @"tap.aif";
notification.alertBody = [NSString stringWithFormat:NSLocalizedString(@"你设置的时间是:%i : %i .",nil),htime1 ,mtime1];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[now1 release];
}
点击确定时触发此方法
-(void)notfacation{
//获取当前时间
sure=YES;
NSDate* now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [calendar components:unitFlags fromDate:now];
hour = [comps hour];
min = [comps minute];
sec = [comps second];
htime1=[textField.text intValue];
mtime1=[textField1.text intValue];
hs=htime1-hour;
ms=mtime1-min;
//设置弹出框提醒用户
UIAlertView *at=[[UIAlertView alloc] initWithTitle:@"!"
message:[NSString stringWithFormat:@"你设置的时间: %i:%i ",htime1,mtime1]
delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"关闭",nil];
[at setDelegate:self];
[at show];
[at release];
}
所设定的时间到了会触发此代理
//到时间时触发的代理
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"123123123131231231++++++++++++");
AudioServicesPlaySystemSound (self.soundFileObject);
sure=NO;
UIApplicationState state = application.applicationState;
if (state == UIApplicationStateActive)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"时间提醒"
message:notification.alertBody
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
iOS下的Notification的使用 Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notification种类,本地和远程。本地的Notification由iOS下NotificationManager统一管理,只需要将封装好的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification,应用只需设计好处理Notification的方法就完成了整个Notification流程了。本地Notification所使用的对象是UILocalNotification,UILocalNotification的属性涵盖了所有处理Notification需要的内容。UILocalNotification的属性有fireDate、timeZone、repeatInterval、repeatCalendar、alertBody、 alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、 soundName和userInfo。UILocalNotification的调度其中fireDate、timeZone、repeatInterval和repeatCalendar是用于UILocalNotification的调度。fireDate是UILocalNotification的激发的确切时间。timeZone是UILocalNotification激发时间是否根据时区改变而改变,如果设置为nil的话,那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发。 repeatInterval是UILocalNotification被重复激发之间的时间差,不过时间差是完全根据日历单位(NSCalendarUnit),例如每周激发的单位,NSWeekCalendarUnit,如果不设置的话,将不会重复激发。 repeatCalendar是UILocalNotification重复激发所使用的日历单位需要参考的日历,如果不设置的话,系统默认的日历将被作为参考日历。UILocalNotification的提醒内容alertBody、alertAction、hasAction和alertLaunchImage是当应用不在运行时,系统处理UILocalNotification提醒是需要的内容。alertBody是一串现实提醒内容的字符串(NSString),如果 alertBody未设置的话,Notification被激发时将不现实提醒。alertAction也是一串字符(NSString),alertAction的内容将作为提醒中动作按钮上的文字,如果未设置的话,提醒信息中的动作按钮将显示为“View”相对文字形式。alertLaunchImage是在用户点击提醒框中动作按钮(“View”)时,等待应用加载时显示的图片,这个将替代应用原本设置的加载图片。hasAction是一个控制是否在提醒框中显示动作按钮的布尔值,默认值为YES。UILocalNotification的其他部分applicationIconBadgeNumber、soundName和userInfo将使UILocalNotification更完整。applicationIconBadgeNumber是显示在应用图标右上角的数字,这样让用户直接了解到应用需要处理的 Notification。soundName是另一个UILocalNotification用来提醒用户的手段,在Notification被激发之后将播放这段声音来提醒用户有Notification需要处理(有声音设置时,声音名称要加上扩展名,如sound1.mp3),如果不设soundName的话,Notification被激发是将不会有声音播放,除去应用特制的声音以外,也可以将soundName设为UILocalNotificationDefaultSoundName来使用系统默认提醒声音。userInfo是Notification用来传递数据的NSDictionary。登记UILocalNotification在设置完UILocalNotification对象之后,应用需要在系统Notification处理队列中登记已设置完的UILocalNotification对象。登记UILocalNotification * localNotification的方式为: [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];在有些时候,应用可能需要直接激发一个Notification而不是等一段时间在激发,应用可以以下的方式直接触发已设好的Notification: [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];处理UILocalNotification在提醒框动作按钮被点击后,应用开始运行时,可以在 -(BOOL)application:didFinishLaunchingWithOptions:这个Application delegate方法中处理。可以通过以下方式来加载为最近未处理的Notification: UILocalNotification * localNotif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];如果应用正在运行时,可以通过覆盖在Application Delegate中的方法-(void)application:didReceiveLocalNotification:来处理 Notification。作为方法的第二个参数为UILocalNotification对象,只需处理对象携带的userInfo来处理响应的动作。取消UILocalNotification可以使用以下两个方式来取消一个已经登记的Notification,第一个方式可以直接取消一个指定的Notification,第二个方式将会把该应用已登记的Notification一起取消 [[UIApplication sharedApplication] cancelLocalNotification:localNotification]; [[UIApplication sharedApplication] cancelAllLocalNotification];一般需要应用程序后台运行时才会显示提示,前台运行时一般不显示提示
。如果想要当应用程序前台应行时也显示提示,则可以通过将下面函数加到appDelegate中实现:- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ UIApplicationState state = application.applicationState;// NSLog(@"%@,%d",notification,state); if (state == UIApplicationStateActive) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:notification.alertBody delegate:self cancelButtonTitle:@"Close" otherButtonTitles:@"OK",nil]; [alert show]; [alert release]; }}总结本地Notification的机制在应用开发中非常有效,可以很好的帮助开发者管理一些指定时间需要发生的事件,例如闹钟类的应用。而且因为系统统一对Notification的管理,让同样的任务可以非常简单得被处理,而无需让应用浪费资源去等待事件的触发