在iPhone中创建基于时间的提醒应用程序 [英] Creating a Time Based Reminder App in iPhone

查看:95
本文介绍了在iPhone中创建基于时间的提醒应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究基于时间的提醒应用。其中用户输入他的提醒和提醒时间。问题是如何连续比较当前时间和用户定义的时间。任何示例代码都会有很大帮助。因为我坚持这一点。

I am working on time based reminder App. in which the user enter his reminder and time for the reminder. The problem is that how to continuously comparing the current time with the user defined time. Any sample code will greatly help. because i am stuck on this point.

推荐答案

比较当前时间与用户定义的时间不是正确的设计模式。

Comparing the current time vs. the user defined one is not the right design pattern.

UIKit提供的NSLocalNotification对象是您任务的更高级抽象。

UIKit offers the NSLocalNotification object that is a more high-level abstraction for your task.

下面是一个的创建和安排在已选定的时间的局部通知代码剪断:

Below is a snip of code that create and schedule a local notification at the choosen time:

    UILocalNotification *aNotification = [[UILocalNotification alloc] init];
    aNotification.fireDate = [NSDate date];
    aNotification.timeZone = [NSTimeZone defaultTimeZone];

    aNotification.alertBody = @"Notification triggered";
    aNotification.alertAction = @"Details";

    /* if you wish to pass additional parameters and arguments, you can fill an info dictionary and set it as userInfo property */
    //NSDictionary *infoDict = //fill it with a reference to an istance of NSDictionary;
    //aNotification.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:aNotification];
    [aNotification release];

此外,请务必将AppDelegate设置为在启动时和正常情况下响应本地通知应用程序的运行时(如果您希望在应用程序处于前台时收到通知):

Also, be sure to setup your AppDelegate to respond to local notifications, both at startup and during the normal runtime of the app (if you want to be notified even the application is in foreground):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    UILocalNotification *aNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey]; 

    if (aNotification) {
        //if we're here, than we have a local notification. Add the code to display it to the user
    }


    //...
    //your applicationDidFinishLaunchingWithOptions code goes here
    //...


        [self.window makeKeyAndVisible];
    return YES;
}



- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

        //if we're here, than we have a local notification. Add the code to display it to the user


}

苹果开发者文档的更多信息

这篇关于在iPhone中创建基于时间的提醒应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆