Creating a Time Based Reminder App in iPhone

2020-02-23 04:42发布

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.

2条回答
爷、活的狠高调
2楼-- · 2020-02-23 05:08

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

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

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


}

More details at the Apple Developer Documentation.

查看更多
姐就是有狂的资本
3楼-- · 2020-02-23 05:16

Why not use NSLocalNotification which you can set for a specified time, much like a calendar event. Alternatively you can add calendar events to the users calendar with EKEventKit

Tutorial for local notifications.

Tutorial for event kit.

查看更多
登录 后发表回答