2017-05-19 11 views
0

私はプログラミングが初めてで、これは私の最初のプロジェクトです。私はかなりシンプルなリマインダーアプリを作っています。 .moreInformation(String)、.fireDate(Date)、.fromDate(Date)、.title(String)、.image(UIImage)というプロパティを持つクラスリマインダーを作成しました。アプリケーション内でこれらのプロパティをすべて編集できます。私の問題はちょうど:私はこのオブジェクトのリマインダを格納するための適切な解決策が必要です。私はそのような私の通知を登録するUserNotificationsを使用しています:通知を受け取ったときに呼び出すことができるように、通知と共にオブジェクトを保存する方法は?

reminder.fireDate = date 
    reminder.image = image 
    reminder.description = descriptionTextView.text 
    reminder.title = titleTextView.text 
    reminder.savedOndate = savedOnDateString 

    let center = UNUserNotificationCenter.current() 
    let category = UNNotificationCategory(identifier: "General", actions: [], intentIdentifiers: [], options: .customDismissAction) 
    center.setNotificationCategories([category]) 

    let content = UNMutableNotificationContent() 
    let contentText = reminder.savedOnDate 
    content.title = "Reminder" 
    content.body = "Your Reminder from the \(contentText) has arrived!" 

    let date2 = reminder.fireDate 
    var components = Calendar.current.dateComponents(in: TimeZone.current, from: date2) 

    components.hour = 18 
    components.minute = 0 

    let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false) 

    let request = UNNotificationRequest(identifier: "Reminder", content: content, trigger: trigger) 


    center.add(request) { (error : Error?) in 
     if let theError = error { 
      print(theError.localizedDescription) 
     } 
    } 

ユーザーが通知を受けたときにアプリを起動したとき、彼はポップアップビューが表示されるはずです、説明を表示する別のビューコントローラに彼をリードしますテキスト、fromDateなど

しかし、どのように通知と共にオブジェクトを保存するので、他のView Controllerは正しい説明テキスト/タイトルなどを表示しますか?

答えて

0

UNMutableNotificationContentインスタンスのuserInfo辞書を使用すると、カスタム情報を保存できます。 userInfo辞書に格納されているオブジェクトは、プロパティリストタイプでなければならないことに注意してください。これは、reminderオブジェクトをそのようなタイプ(たとえばNSDictionary)に変換する必要があるか、またはこれがクリーナーソリューションになることを意味し、reminderオブジェクトのクラスにNSCodingを実装し、NSKeyedArchiver/NSKeyedUnarchiverを使用してエンコードまたはエンコードします。

+0

ありがとうございました! –

+0

と、通知を受け取った後はどうすればいいですか? –

+0

あなたが受け取る 'UNNotification'オブジェクトには、' UNNotificationRequest'型の 'request'プロパティがあります。これは' userInfo'ディクショナリを含む 'UNNotificationContent'型の' content'プロパティを持ちます。通知の詳細については、[セッション707](https://developer.apple.com/videos/play/wwdc2016/707/)および[セッション708](https://developer.apple.com/videos/play/wwdc2016/708 /)を昨年のWWDCから取得しました。 – floschliep

関連する問題