开发者

How do I present an alarm to a user, even when my application is inactive?

I want to make an app whi开发者_Go百科ch will let you set an alarm inside my iPhone app, then have it be activated even though my app is not running.

How would I implement this?


You want to use UILocalNotification. A couple things to note before you dive in:

  1. It's only available on iOS4 and up.
  2. NSDate is a pain in the ass, and it's the only option for scheduling

With that said, you can begin:

 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];

If you need any more help, just comment :)


try this:

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

      }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜