2016-01-08 58 views
1

主要问题:如果我想要将截止日期附加到我的应用中的任务,我应该使用本地通知,警报还是提醒吗?即使截止日期到达时应用程序未运行,我也希望他们收到通知。在iOS8-iOS9中设置提醒/警报

我发现this教程使用UILocalNotification它说可以:

给我们投通知给用户的能力,而不会

然而,有人写了6年前运行的应用程序并且还声明这是iOS4中引入的。我知道5个iOS版本有很多变化。

我也read,或者,我可以使用事件工具包。但是,这似乎比UILocalNotification更复杂。

最后,我可以使用可能采取当前日期/时间和提醒日期/时间和create a timer倒计时。

因此,如果我只是想在应用程序中附加到期日期的任务(他们不需要在提醒或日历中显示),那么最佳方法是什么?为什么?

+1

是的,你应该使用'UILocalNotification'。 – rptwsthi

+1

你必须使用UILocalNotification。 – darshan

+0

谢谢你们两位。由于我发现的教程是在Objective-C中,如果你们中的任何一个想要提供在** Swift **中创建UILocalNotification的示例,我可以将其标记为正确的答案。或者可能是一个区分UILocalNotification使用和警报使用之间区别的答案,那也可以。 - @rptwsthi –

回答

1

我发现下面的大块code here。这也是如何在应用程序中创建事件的很好的教程。

var notification = UILocalNotification() 
notification.alertBody = "Todo Item \"\(item.title)\" Is Overdue" // text that will be displayed in the notification 
notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view" 
notification.fireDate = item.deadline // todo item due date (when notification will be fired) 
notification.soundName = UILocalNotificationDefaultSoundName // play default sound 
notification.userInfo = ["UUID": item.UUID, ] // assign a unique identifier to the notification so that we can retrieve it later 
notification.category = "TODO_CATEGORY" 
UIApplication.sharedApplication().scheduleLocalNotification(notification)