iPhone - UILocalNotification作为闹钟

时间:2011-07-14 16:05:19

标签: iphone alarm uilocalnotification

即使我的iPhone应用程序处于后台,我如何使用UILocalNotification每天晚上8点显示我的alram?

3 个答案:

答案 0 :(得分:3)

fireDate设置为8.00 PM并将repeatInterval设置为NSDayCalendarUnit,并使用[[UIApplication sharedApplication] scheduleLocalNotification: myNotification];

安排提醒

答案 1 :(得分:2)

dasdom回答是正确的。只是想添加它,如果您的设备处于静音模式,闹钟将不会发出声音。这是Apple对UILocalNotification的限制。

答案 2 :(得分:0)

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

 NSCalendar *calendar=[NSCalendar currentCalendar];
    [calendar setTimeZone:[NSTimeZone defaultTimeZone]];

    unsigned currentFlag=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSWeekdayCalendarUnit;

    NSDateComponents *comp=[calendar components:currentFlag fromDate:[NSDate date]];

    comp.hour=8;
    comp.minute=0;
    comp.second=0;

    NSDate *date=[[calendar dateFromComponents:comp]dateByAddingTimeInterval:0];

    if (localNotification==nil) {
        return;
    }

    localNotification.fireDate=date;
    localNotification.timeZone=[NSTimeZone defaultTimeZone];
    localNotification.repeatCalendar=[NSCalendar currentCalendar];

    localNotification.alertBody=@"Good Morning dude..!";

    localNotification.alertAction=@"Snooze";

    localNotification.repeatInterval=NSDayCalendarUnit;

    localNotification.soundName=@"goodmorning.caf";

[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
  

我希望这会对你有帮助......!