鬧鐘的話 要入住系統隊列的.你可以通過設定本地消息給系統實作鬧鐘的效果. 時間可以是一次性的 可以是周期性的 具體可以看看文檔
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的管理,讓同樣的任務可以非常簡單得被處理,而無需讓應用浪費資源去等待事件的觸發