xcode 在 iPhone 中创建基于时间的提醒应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8892619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:01:15  来源:igfitidea点击:

Creating a Time Based Reminder App in iPhone

iphoneobjective-cxcodensdatedate-comparison

提问by Fahad Jamal

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.

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

回答by 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.

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

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):

此外,请务必设置您的 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


}

More details at the Apple Developer Documentation.

更多详细信息,请参阅Apple 开发人员文档

回答by Simon Lee

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

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

Tutorial for local notifications.

本地通知教程。

Tutorial for event kit.

活动工具包教程。