ios - 如何在 Swift 的日期选择器上设置本地通知时间

标签 ios swift notifications uidatepicker

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

我是使用 Swift 进行应用程序开发的新手,我正在尝试创建一个简单的应用程序,当用户在日期选择器上进行选择时,该应用程序会向用户发送本地通知。由于某种原因,UNCalenderNotificationTrigger 将仅采用 DateComponents() 并且 DateComponents() 必须是 Int,而不是 Date 类型()(来自日期选择器)。有人可以帮我吗?谢谢

最佳答案

您可以使用 Calendar 使用

Date 对象获取 DateComponents
let componentsFromDate = Calendar.current.dateComponents(in: TimeZone.current, from: Date())

如果将 Date() 替换为 DatePicker 中的 Date 对象,则可以使用 componentsFromDate创建UNCalendarNotificationTrigger

关于ios - 如何在 Swift 的日期选择器上设置本地通知时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45603436/

相关文章:

ios - 如何在 iOS 中使用 ARM 内在函数?

objective-c - 检查NSString是否包含全部或某些字符

ios - 具有动态大小内容的 UIScrollView

ios - 位置返回零

javascript - Chrome : Notifications not working, 未请求权限

ios - 当用户按下图标而不是警报时处理本地通知

Android:取消通知的通知操作

ios - Objective-C 是否超时更改

iphone - 使用自动布局更改约束

ios - 多个 NSTimers Xcode 6 Swift