2012-01-17 49 views
0

我正在基于时间提醒应用程序。其中用户输入他的提醒和提醒的时间。问题是如何持续比较当前时间和用户定义的时间。任何示例代码将极大地帮助。因为我被困在这一点上。在iPhone中创建基于时间的提醒应用程序

+0

你可以从线程帮助http://stackoverflow.com/questions/949416/how-to-compare-two-dates-in-objective-c – Ali3n 2012-01-17 10:04:42

+0

我希望该方法将持续运行,这将方法就像位置管理器的didupdatetolocation一样被调用 – 2012-01-17 10:13:50

回答

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双方在启动和在正常本地通知作出回应,应用程序的运行时间(如果你希望收到即使应用程序在前台):在Apple Developer Documentation

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


} 

更多细节。

2

为什么不使用NSLocalNotification您可以指定的时间设置,就像日历事件。另外,您可以添加日历事件的用户日历EKEventKit

教程local notifications

教程event kit

相关问题