0

我正在开发基于时间的提醒应用程序。用户在其中输入他的提醒和提醒时间。问题是如何不断地将当前时间与用户定义的时间进行比较。任何示例代码都会有很大帮助。因为我被困在这一点上。

4

2 回答 2

15

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

UIKit 提供了 NSLocalNotification 对象,它对你的任务来说是一个更高层次的抽象。

以下是在选定时间创建和安排本地通知的代码片段:

    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 以响应本地通知,无论是在启动时还是在应用程序的正常运行期间(如果您想在应用程序处于前台时也收到通知):

- (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


}

Apple 开发者文档中的更多详细信息。

于 2012-01-17T10:13:35.573 回答
2

为什么不使用NSLocalNotification可以设置指定时间的,就像日历事件一样。或者,您可以将日历事件添加到用户日历EKEventKit

本地通知教程。

活动套件教程。

于 2012-01-17T10:05:28.120 回答