ios - 在所有工作日触发本地 iOS 通知

标签 ios swift

我正在尝试设置在所有工作日触发的本地通知,即:从周一到周五下午 5 点,但不在周末。
在 Apple 的文档中,我发现这种方式可以触发所有天的通知:

var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current

dateComponents.hour = 17    // 17:00 hours

// Create the trigger as a repeating event.
let trigger = UNCalendarNotificationTrigger(
dateMatching: dateComponents, repeats: true)

但我无法找到如何排除特定日期,即:星期六和星期日,并将时间设置为下午 5 点。有线索吗?

最佳答案

首先创建一个日期对象

let date = Date(timeIntervalSinceNow: 3600) // it may be any date whatever you want 

从日期组件创建触发器:

let triggerWeekly = Calendar.current.dateComponents([.weekday,hour,.minute,.second,], from: date)

现在在 UNCalendarNotificationTrigger 中通过 repeats true 传递那个日期

let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)

创建一个带时间的日期:

let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm"
let someDateTime = formatter.date(from: "2016/10/08 22:31") // put your date and time here

如果 Locale 特定时间:

 formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

关于ios - 在所有工作日触发本地 iOS 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57060780/

相关文章:

c# - 为什么我的 Unity3D Sprite 在 iPhone 上渲染时看起来像素化?

swift - 创建一个Publisher,一一通知订阅者,互相等待

ios - 每次我滚动 cell.viewWithTag 都会在展开发现 nil 时给出 fatal error

ios - 如何快速录制屏幕视频?

iOS 11 tableView with Style Grouped 使大标题失效

ios - 在 Objective-C 中搜索大型数组

PHP 引用可打印电子邮件和 Apple Mail App

swift - Swift 中的转换未按预期工作

ios - map 可能不会缩小那么远

ios - 只有根级导航目的地对具有同构路径的导航堆栈有效