2017-08-10 41 views
0
import UIKit 
import UserNotifications 


class ViewController: UIViewController { 

    @IBOutlet weak var myDatePicker1: UIDatePicker! 
    @IBAction func action(_ sender: Any) { 
     let content = UNMutableNotificationContent() 
     content.title = "This is a notification" 
     content.subtitle = "I hope this works" 
     content.body = "from the tutorial" 
     content.badge = 1 

     let componentsFromDate = Calendar.current.dateComponents(in: TimeZone.current, from: myDatePicker1.date) 

     let trigger = UNTimeIntervalNotificationTrigger(timeInterval: componentsFromDate, repeats: true) 

     let request = UNNotificationRequest(identifier: "timeDone", content: content, trigger: trigger) 
     UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 

    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in}) 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

} 

下面是在控制檯什麼表明,當我打印的請求,並觸發:用斯威夫特和應用程序開發如何本地通知時間設置爲時間日期選擇器的雨燕

<UNNotificationRequest: 0x6000002335e0; identifier: timeDone, content: 
<UNNotificationContent: 0x600000113ef0; title: This is a notification, 
subtitle: I hope this works, body: from the tutorial, 
categoryIdentifier: , launchImageName: , peopleIdentifiers: (), 
threadIdentifier: , attachments: (), badge: 1, sound: (null), 
hasDefaultAction: YES, defaultActionTitle: (null), 
shouldAddToNotificationsList: YES, 
shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, 
shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, 
darwinNotificationName: (null), darwinSnoozedNotificationName: (null), 
trigger: <UNCalendarNotificationTrigger: 0x600000233540; 
dateComponents: <NSDateComponents: 0x6000003461a0> 
    Calendar: <CFCalendar 0x60000009c7a0 [0x10c9fee40]>{identifier = 'gregorian'} 
    TimeZone: America/Vancouver (PDT) offset -25200 (Daylight) 
    Era: 1 
    Calendar Year: 2017 
    Month: 8 
    Leap month: no 
    Day: 11 
    Hour: 9 
    Minute: 58 
    Second: 39 
    Nanosecond: 0 
    Quarter: 0 
    Year for Week of Year: 2017 
    Week of Year: 32 
    Week of Month: 2 
    Weekday: 6 
    Weekday Ordinal: 2, repeats: YES>> 

我是新我試圖創建一個簡單的應用程序,當他們在日期選擇器上選擇時,向用戶發送本地通知。由於某些原因,UNCalenderNotificationTrigger將只需要DateComponents()DateComponents()必須是Int,而不是Date()(來自日期選擇器)。有人可以幫我嗎?由於

+0

請向我們展示一些關於迄今爲止所做工作的代碼。 –

回答

0

您可以從Date對象使用Calendar使用

let componentsFromDate = Calendar.current.dateComponents(in: TimeZone.current, from: Date()) 

得到DateComponents如果你與你Date對象從DatePicker更換Date(),您可以使用componentsFromDate創建UNCalendarNotificationTrigger

+0

感謝您的幫助,但出於某種原因,這沒有奏效......我得到的錯誤消息是「無法將類型」DateComponents「的值轉換爲鍵入」TimeInterval「(又名Double)」。 –

+0

你甚至使用'UNCalendarNotificationTrigger'?聽起來像你正在使用'UNTimeIntervalNotificationTrigger'。 –

+0

嗯,是的,你是對的。我正在那樣做。然而,我現在修好了,沒有錯誤,但由於某種原因,在模擬器中,沒有通知發射。上面的代碼不會更新到日曆觸發器而不是時間間隔觸發器。還有什麼我正在做的錯誤是阻止這個工作。順便提一下,感謝您的所有幫助。我正在學習很多 –

相關問題