Creating a Time Based Reminder App in iPhone

佐手、 提交于 2019-11-30 04:08:25
valvoline

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!