-1

我想要一个用于报警的小型 iOS 应用程序。一旦我选择了特定的日期和时间,就应该发出通知。

如果我选择所有天,每天都应该发出警报。如果您已经有代码,请您帮我处理这个应用程序并发送给我。

先感谢您。

4

2 回答 2

2

试试下面的代码:

UILocalNotification *localNotif = [[UILocalNotification alloc]init];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *datePickerDate = [NSDate date];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:datePickerDate];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:datePickerDate];

NSDateComponents *dateComps = [[NSDateComponents alloc]init];
[dateComps setYear:[dateComponents year]];
[dateComps setMonth:[dateComponents month]];

[dateComps setHour:[timeComponents hour] ];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:00];

NSDate *fireDate = [calendar dateFromComponents:dateComps];
//NSLog(@"%@",fireDate);
localNotif.fireDate = fireDate;
localNotif.soundName = UILocalNotificationDefaultSoundName;
//localNotif.repeatInterval = 1.0;
localNotif.alertAction = @"Alarm";
//NSLog(@"%@",alarmId);

//for alarm once
if ([alarm.repeat_mode_id intValue] == 0) {


} else if([alarm.repeat_mode_id intValue] == 1) { //for alarm every day
    localNotif.repeatInterval = NSDayCalendarUnit;
}
localNotif.alertBody = alarm.alarm_label;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
于 2013-06-11T04:50:27.790 回答
0

请使用以下步骤每天创建警报。

  1. 您创建警报并将此警报存储在 NSUSerDefaults 或数据库中。例如:开火日期:12-11-2013 10.30AM

  2. 一旦警报完成。即收到警报,在已存储的日期值中更新一天。

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0)
    {
         UIApplicationState state = [[UIApplication sharedApplication] applicationState];
          if (state == UIApplicationStateActive)
        {
             // The Alarm Received The Pointer come to This.
        }
    else
    {
         // You click notification. the pointer come in else condition
     NSDate *alerm_time_str=[alarmData valueForKey:@"FireDate"];
    
    
     NSDate *fire_date;
    
            if ([alerm_time_str timeIntervalSinceNow] < 0.0) {
    
                NSLog(@"Date is Passed");
    
                int daysToAdd = 1;
                NSDate *newDate1 = [alerm_time_str dateByAddingTimeInterval:60*60*24*daysToAdd];
                fire_date=newDate1;
    
            }
            else
            {
                fire_date=alerm_time_str;
            }
    

    }

如果有任何澄清让我。

于 2013-06-11T04:54:31.633 回答