即使我的应用程序处于非活动状态,如何向用户显示警报?

时间:2011-01-31 06:31:40

标签: iphone cocoa-touch ios

我想创建一个应用程序,让您在我的iPhone应用程序中设置闹钟,然后即使我的应用程序没有运行也会激活它。

我该如何实现?

2 个答案:

答案 0 :(得分:7)

您想使用UILocalNotification。在潜入之前需要注意几点:

  1. 仅适用于iOS4及更高版本。
  2. NSDate是一个痛苦的屁股,它是安排
  3. 的唯一选择

    话虽如此,你可以开始:

     UILocalNotification *notif = [[UILocalNotification alloc] init];
    
     notif.alertBody = @"This is a Local Notification";
     NSDate *date = [NSDate date];
     notif.fireDate = [date addTimeInterval:600];// fire the notification in ten minutes
     [[UIApplication sharedApplication] scheduleLocalNotification:notif];
     [notif release];
    

    如果您需要更多帮助,请发表评论:)

答案 1 :(得分:1)

试试这个:

in .h

int *currentTime;

in .m

-(void)startTimer {

     NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES];

}

-(void)updateCurrentTime {

      currentTime ++;
      if (currentTime == userDefinedTime) {

            // Time is up

      }

}
相关问题