2016-01-08 116 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)