Widget needs to be auto refreshed at 6AM & 6PM every day. I have added the following logic/code based on the widget adding time. But I could not see the widget is auto refreshing at 6AM & 6PM
let calendar = Calendar.current
let TimeComponentfor6AM = DateComponents(calendar: calendar, hour:6)
let TimeComponentfor6PM = DateComponents(calendar: calendar, hour: 18, minute: 00)
let now = Date()
let startOfToday = calendar.startOfDay(for: now)
let startTime = calendar.date(byAdding: TimeComponentfor6AM, to: startOfToday)!
let endTime = calendar.date(byAdding: TimeComponentfor6PM, to: startOfToday)!
if now < startTime {//if widget adds before 6AM
if let At6AM = Calendar.current.date(bySettingHour: 6, minute: 0, second: 0, of: Date()) {
let refreshEntryFor6AM = RecommendedWidgetEntry(date: At6AM, recommedationData: recFeedModel)
refreshEntries.append(refreshEntryFor6AM)
}
if let At6PM = Calendar.current.date(bySettingHour: 18, minute: 0, second: 0, of: Date()) {
let refreshEntryFor6PM = RecommendedWidgetEntry(date: At6PM, recommedationData: recFeedModel)
refreshEntries.append(refreshEntryFor6PM)
}
return refreshEntries
}
else if now < endTime { //if the widget adds before 6PM
if let At6PM = Calendar.current.date(bySettingHour: 18, minute: 0, second: 0, of: Date()) {
let refreshEntryFor6PM = RecommendedWidgetEntry(date: At6PM, recommedationData: recFeedModel)
refreshEntries.append(refreshEntryFor6PM)
}
let today = Date()
if let nextDayDate = Calendar.current.date(byAdding: .day, value: 1, to: today)
{
if let At6AM = Calendar.current.date(bySettingHour: 6, minute: 0, second: 0, of: nextDayDate) {
let refreshEntryFor6AM = RecommendedWidgetEntry(date: At6AM, recommedationData: recFeedModel)
refreshEntries.append(refreshEntryFor6AM)
}
}
return refreshEntries
}
else { // if widget added post 6PM
let today = Date()
if let nextDayDate = Calendar.current.date(byAdding: .day, value: 1, to: today)
{
if let At6AM = Calendar.current.date(bySettingHour: 6, minute: 0, second: 0, of: nextDayDate) {
let refreshEntryFor6AM = RecommendedWidgetEntry(date: At6AM, recommedationData: recFeedModel)
refreshEntries.append(refreshEntryFor6AM)
}
if let At6PM = Calendar.current.date(bySettingHour: 18, minute: 0, second: 0, of: nextDayDate) {
let refreshEntryFor6PM = RecommendedWidgetEntry(date: At6PM, recommedationData: recFeedModel)
refreshEntries.append(refreshEntryFor6PM)
}
}
Kindly help me on it if anything missed or need to be added