2012-04-07 58 views
0

我阅读了UILocalNotification的文档,该示例显示使用NSDateComponents,然后设置星期,日期,月份等,以安排通知。我基本上是想做一些类似闹钟的事情,在特定的时间每天重复一次。我有一段时间在UIPickerView,并从那时为我的对象创建一个NSDate。所以我抓紧时间,我试图安排通知如下:使用NSDate的UILocalNotification

NSDate *date = [dateFormatter dateFromString:s]; 
    NSCalendar *calender = [NSCalendar currentCalendar]; 
    NSLog(@"%@", s); 
    UILocalNotification *note = [[UILocalNotification alloc] init]; 
    note.alertAction = alarm.name; 
    note.alertBody = alarm.note; 
    note.fireDate = date; 
    note.timeZone = [[NSCalendar currentCalendar] timeZone]; 
    note.repeatInterval = NSDayCalendarUnit; 
          note.timeZone = [NSTimeZone systemTimeZone]; 

    [[UIApplication sharedApplication] scheduleLocalNotification:note]; 

我可以这样做吗?或者我必须使用NSDateComponents。我的代码目前不会触发通知,我想知道是否可以通过这种方式触发通知,或者如果您必须使用NSDateComponents。

另外,如果我确实需要使用NSDateComponents,那么我该如何才能获得当天的日期,以便我可以设置当天的闹钟,并且我假设从那里repeatInterval将在接下来的日子生效。

非常感谢!

回答

0

只要启动是一个有效的NSDate通知将触发。这是我在App中使用的代码

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

     localNotification.fireDate = [NSDate date]; 


     localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
     localNotification.alertBody = @"some string"; 
     localNotification.repeatInterval = NSWeekCalendarUnit; 
     localNotification.repeatCalendar = [NSCalendar currentCalendar]; 
     localNotification.soundName = UILocalNotificationDefaultSoundName; 

     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

检查您传递给传播的日期是一个有效的NSDate。我不确定您使用的时区重复代码是否需要?它可能会造成问题。