当我执行以下操作时(2017年1月7日):
let dateComponents = Calendar.current.dateComponents(in: TimeZone.current, from: date)
print("dateComponents = \(dateComponents)")
let trigger = UNCalendarNotificationTrigger(dateMatching:dateComponents, repeats: false)
let nextDate = trigger.nextTriggerDate()
print("nextDate = \(nextDate)")
Run Code Online (Sandbox Code Playgroud)
然后我得到以下输出:
dateComponents = calendar:gregorian(current)timeZone:Europe/Stockholm(当前)时代:1年:2017月:1天:8小时:21分钟:34秒:0纳秒:0个工作日:1个工作日原点:2个季度:0个星期一个月:1周年份:1年ForWeekOfYear:2017年是LeapMonth:false
nextDate = nil
问题:为什么trigger.nextTriggerDate() = nil?
更新:我觉得我dateComponents可能会被超定.因此nextEvent,我介绍了一个只包含dateComponents的日期时间和分钟:
var nextEvent = DateComponents()
nextEvent.day = dateComponents.day
nextEvent.hour = dateComponents.hour
nextEvent.minute = dateComponents.minute
let trigger = UNCalendarNotificationTrigger(dateMatching:nextEvent, repeats: false)
Run Code Online (Sandbox Code Playgroud)
当我现在调用trigger.nextTriggerDate()它成为
nextDate = Optional(2017-01-08 20:34:00 +0000)
Run Code Online (Sandbox Code Playgroud)
正如它应该.但是我不明白为什么在创建触发器时我不能使用dateComponents.
我正在尝试在 iOS 11 应用程序中实现一个设置屏幕,我需要一个用于控制用户通知的 UISwitch。当设置为关闭时,我想放弃通知权限,当设置为打开时,我想请求权限(标准对话框要求用户允许发送她的通知)。
为了请求许可,我找到了以下代码:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在系统设置中关闭应用程序的通知,此代码不会弹出带有请求的对话框,它只是简单地在granted.
我找不到有关如何放弃权限的任何信息。
有关如何解决问题的任何提示?甚至有可能吗,或者苹果是否认为这个任务应该只留给系统设置?
我有一个用 Swift 编写的应用程序,它使用 UNUserNotificationCenter,当应用程序处于前台时,我让它显示通知。
我想要做的是在发送通知并且应用程序处于前台后更新 UI 下面显示的通知很好,但是当它显示时,我还想执行一个名为 updateUI() 的函数作为通知日期在我的用户界面中,我想在通知出现后立即清除它。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何在完成处理程序中添加对 udateUI() 的调用。任何人都可以提供帮助吗?
在iOS 10中,有一个选项可以在应用程序处于前台时使用UNNotificationPresentationOptions显示通知,
但是我找不到任何关于如何使用它的示例,请提出一些关于如何实现此功能的建议