Swift 4 How to get all events from calendar? - ios

I am using Swift 4.1. And I want to write a function which will collect all events from all calendars in Calendar app of iOS. Thanks to this answer on stackoverflow: How to get all Events out of a Calendar (Swift) I was able to write my own class and call it Cale. Please, look at this:
import UIKit
import EventKit
class Cale {
private func createDate(year: Int) -> Date {
var components = DateComponents()
components.year = year
components.timeZone = TimeZone(secondsFromGMT: 0)
return Calendar.current.date(from: components)!
}
private let eventStore = EKEventStore()
private func get() {
let calendars = eventStore.calendars(for: .event)
for calendar in calendars {
// This checking will remove Birthdays and Hollidays callendars
guard calendar.allowsContentModifications else {
continue
}
let start = createDate(year: 2016)
let end = createDate(year: 2025)
print("start: \(start)")
print(" end: \(end)")
let predicate = eventStore.predicateForEvents(withStart: start, end: end, calendars: [calendar])
print("predicate: \(predicate)")
let events = eventStore.events(matching: predicate)
for event in events {
print(" title: \(event.title!)")
print("startDate: \(event.startDate!)")
print(" endDate: \(event.endDate!)")
}
}
}
func checkStatusAndGetAllEvents() {
let currentStatus = EKEventStore.authorizationStatus(for: EKEntityType.event)
switch currentStatus {
case .authorized:
//print("authorized")
self.get()
case .notDetermined:
//print("notDetermined")
eventStore.requestAccess(to: .event) { accessGranted, error in
if accessGranted {
self.get()
} else {
print("Change Settings to Allow Access")
}
}
case .restricted:
print("restricted")
case .denied:
print("denied")
}
}
}
Quite simple class, you can use it, it is working but with one exception.
The main function there is get() In this function I am creating predicate based on two dates: start and end. As you can see start date is:
2016-01-01 00:00:00 +0000
and end date is:
2025-01-01 00:00:00 +0000
But if we run the program we will see that predicate will be like this:
CADEventPredicate start:01/01/2016, 03:00; end:01/01/2020, 03:00;
cals:(
2 )
Only from 2016 to 2020, 4 years! I have tested it on different dates, but I could get predicate with 4 years interval maximum. It means, it will not give me all events! So question is: How to get all events from calendar? If it possible, without using dates!
Thank you for any future help or advice!

In case anyone is still wondering, Apple limited this to four years intentionally.
From predicateForEvents(withStart:end:calendars:):
For performance reasons, this method matches only those events within a four year time span. If the date range between startDate and endDate is greater than four years, it is shortened to the first four years.
If you want a range longer than that, I guess you'll have to wrap this in your own function to split it into four year chunks.

After spending so much time, i got a solution to my problem. My problem was as below,
event start date 2019-03-15 09:00:00 +0000
event end date 2019-03-15 14:00:00 +0000
the predicate as below,
CADEventPredicate start:15/03/19, 1:30 PM; end:15/03/19, 7:30 PM; cals:(null)
As i am from India, the predicate is adding GMT+5:30 to my actual event start and end time. I got a method from stack overflow, to convert to local & global timezone as below.
extension Date {
// Convert local time to UTC (or GMT)
func toGlobalTime() -> Date {
let timezone = TimeZone.current
let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}
// Convert UTC (or GMT) to local time
func toLocalTime() -> Date {
let timezone = TimeZone.current
let seconds = TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}
}
While passing start and end date with time to predicate i am converting to globalTimeZone as below.
let predicate = eventStore.predicateForEvents(withStart: eventStartDateTime.toGlobalTime(), end: eventEndDateTime.toGlobalTime(), calendars: nil)
If you don't have time, you have only start & end dates, then use predicate as below,
event start date 2019-03-15 00:00:00 +0000
event end date 2019-03-15 00:00:00 +0000
let predicate = eventStore.predicateForEvents(withStart: eventStartDateTime, end: eventEndDateTime, calendars: nil)
Because if you don't have time and converting to globalTimeZone, you may face issues in deleting the events.
I hope it will help some one.

Related

Convert UTC time to PST in SWIFT

I need to convert the UTC time to PST
From backed, I get UTC dates like "2021-06-25T07:00:00Z"
I need to show the dates in Hstack from Provided UTC date to the current date.
I write the following code.
Anyone help to me.
func datesRange(from:Date, to:Date)->[Date]{
if from > to {return [Date]()}
var tmpdate = from
var array:[Date] = []
while tmpdate <= to {
array.append(tmpdate)
tmpdate = Calendar.current.date(byAdding: .day,value: 1, to: tmpdate)!
}
return array
}
extension Date{
func convertTimezone(timezone:String)-> Date{
if let targettimeZone = TimeZone(abbreviation: timezone){
let delta = TimeInterval(targettimeZone.secondsFromGMT(for: self) - TimeZone.current.secondsFromGMT(for: self))
return addingTimeInterval(delta)
}else{
return self
}
}
}
I used as follows
func getrangeDays(){
let startday = "2021-06-25T07:00:00Z"
let dateformater = DateFormatter()
dateformater.locale = Locale(identifier: "en_US_POSIX")
dateformater.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
if let date = dateformater.date(from: startday){
let rangedays = datesRange(from:date.convertTimezone(timezone: "PST") , to: Date().convertTimezone(timezone: "PST"))
print(rangedays)
}
}
Your convertTimezone() function does not make sense. It is trying to convert a Date to a different time zone. A Date object does not have a time zone. It is an instant in time, anywhere on the planet. Time zones only make sense when you want to display a Date, or do time zone specific date calculations. (And in that case you want to create a Calendar object and set its time zone to the desired time zone, then use that Calendar for your date calculations.)
Get rid of that function.
Convert your input date string to a Date as you are doing now (although you might want to use an ISO8601DateFormatter rather than a regular date formatter, since those are specifically intended for handling ISO8601 dates.)
Build your date range using your datesRange() function.
Then use a second DateFormatter to display your dates in PST. (Not convert Dates to PST. That doesn't make sense.)

How to check if date is at least previous day?

Hi everyone I'm trying to get a positive answer when comparing a date earlier than today
To do this I am using a Date extension with this boolean
extension Date {
var isPreviousDate: Bool {
return self.timeIntervalSinceNow.sign == .minus
}
}
Here is my problem .. when I print the date to compare it tells me that today's date is earlier and I don't understand why
Since I'm having problems I tried to create a current date and compare it with today's date the answer is that today's date is earlier than today ... this is weird because it shouldn't tell me it's older
This is how I create the date to compare
var calendar = Calendar(identifier: Calendar.current.identifier)
calendar.timeZone = NSTimeZone(name: "UTC")! as TimeZone
guard let selectedDate = calendar.date(from: DateComponents(year: Date().currentYear, month: Date().currentMonth, day: Date().currentDate)) else { return }
print("isPrevious Date :",selectedDate.isPreviousDate)
print(selectedDate)
am I doing something wrong?
this is what I read in the console when I print the created date to compare
isPrevious Date: true
2021-02-11 00:00:00 +0000
timeIntervalSinceNow is a FloatingPoint value with a very high precision, that represents seconds passed since the Date.
To check if current date is at least on a previous day, you could do something like this:
extension Date {
var isAtLeastPreviousDay: Bool {
return isPast && !isToday
}
private var isPast: Bool {
return self < Date()
}
private var isToday: Bool {
return Calendar.current.isDateInToday(self)
}
}
The issue there is that you are using a custom calendar using UTC timezone. You should use the current calendar with the current timezone. Note that you can use Calendar method startOfDay to get the start of day of a specific date:
extension Date {
var isPreviousDate: Bool {
timeIntervalSinceNow.sign == .minus
}
var startOfDay: Date {
Calendar.current.startOfDay(for: self)
}
}
let now = Date()
let startOfDay = now.startOfDay
print("isPrevious Date:", startOfDay.isPreviousDate) // true
If you would like to check if a date is in yesterday all you need is to use calendar method isDateInYesterday:
extension Date {
var isDateInYesterday: Bool {
Calendar.current.isDateInYesterday(self)
}
}
Date().isDateInYesterday // false
Date(timeIntervalSinceNow: -3600*24).isDateInYesterday // true

Swift 4 check whether DST is applicable for given timezone? [duplicate]

I need to check to see if the current date is during daylight savings time. In pseudocode that would be like this:
let date = NSDate()
if date.isDaylightSavingsTime {
print("Success")
}
I haven't been able to find the solution to this anywhere on the internet.
An NSDate alone represents an absolute point in time.
To decide if a date is during daylight savings time or not
it needs to be interpreted in the context of a time zone.
Therefore you'll find that method in the NSTimeZone class and not
in the NSDate class. Example:
let date = NSDate()
let tz = NSTimeZone.localTimeZone()
if tz.isDaylightSavingTimeForDate(date) {
}
Update for Swift 3/4:
let date = Date()
let tz = TimeZone.current
if tz.isDaylightSavingTime(for: date) {
print("Summertime, and the livin' is easy ... 🎶")
}
Swift 4.0 or later
You can check a date isDaylightSavingTime in two ways by time zone identifier or abbreviation.
let timeZone = TimeZone(identifier: "America/New_York")!
if timeZone.isDaylightSavingTime(for: Date()) {
print("Yes, daylight saving time at a given date")
}
let timeZone = TimeZone(abbreviation: "EST")!
if timeZone.isDaylightSavingTime(for: Date()) {
print("Yes, daylight saving time at a given date")
}

How to check the amount of time between two NSDates?

I'm making a kind of challenge based app that requires that the user comes back every day. If he misses one day, he has to start all over again.
My problem is my dateChanged()-function; the first thing is, that it doesn't work very reliable, the second is that I just check if the date changed, I accordingly don't know if there were one or two days between using the app.
Here's my current function:
public func changeDays()
{
myFormatter.dateStyle = .short
myFormatter.locale = Locale(identifier: "de_DE")
oldDate = defaults.string(forKey: "oldDate")!
let newDate = Date()
let newDateString = myFormatter.string(from: newDate)
if newDateString == oldDate
{
NumberOfDaysInARow.text = "\(days) / 30"
}
else if newDateString != oldDate
{
days += 1
NumberOfDaysInARow.text = "\(days) / 30"
defaults.set(days, forKey: "days")
}
oldDate = newDateString
defaults.set(oldDate, forKey: "oldDate")
}
Just today it started giving me a fatal error when starting the app on my iPhone, did not happen in the simulator though... weird.
How do I have to change my function to make it a 100% reliable (and working) while also being able to check the amount of time between the two dates?
Thank you for having a look! Have a great day
You could extend Date with the function below that returns the amount of days from another date.
extension Date {
// Returns the amount of days from another date
func days(from date: Date) -> Int {
return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
}
}
Instead of saving oldDate as a string you can set it to defaults as a date:
defaults.set(Date(), forKey: "oldDate")
You can get the old date from defaults using:
let oldDate = defaults.object(forKey:"oldDate") as! Date
Once you have your old date
let dateNow = Date()
let timeDifference = dateNow.days(from: oldDate!)
If timeDifference > 1 {
// older than 1 day
} else {
// Streak still alive
}
}
If you look in the documentation you will see that Date has a method whose sole purpose is too determine the interval between two dates time​Interval​Since(_:​).
If you set the old date to always be 11:59PM on the day it was last used you only have to see if the interval is greater than 24 hours worth of seconds (60 seconds * 60 minutes * 24 hours).
You may want to look at the docs for DateComponents for help creating a date that uses the current date but with a specific time.

How do I retrieve an EKEvent that is greater than 1 month old?

using this code:
func sampleCase() {
var dateConstructor = DateComponents()
dateConstructor.year = 2016
dateConstructor.month = 12
dateConstructor.day = 15
let calendar: Calendar = Calendar.current
let workCalendar = CalendarUtilities.calendar("WorkSchedule", using: eventStore)
let referenceDate = calendar.date(from: dateConstructor)!
let startDate = referenceDate
let endDate = referenceDate.addingDays(1) // midnight
let range = DateRange(start: startDate, andEnd: endDate)
let predicate = eventStore.predicateForEvents(withStart: range.startDate, end:range.endDate, calendars:[workCalendar])
let events = eventStore.events(matching: predicate)
print("Found: \(events.count) events")
print("used:\(startDate)")
print("\(endDate)")
}
I only get correct results for events in the previous month... (These examples use dates that have events and should return non-zero values.)
authorization was granted...
Found: 1 events
used:2016-12-15 05:00:00 +0000
2016-12-16 05:00:00 +0000
going back earlier results in 0 events.
I also tried using enumeration with similar results:
eventStore.enumerateEvents(matching: predicate) { (result, stop) in
counter += 1
}
print("found \(counter) items using iterator")
used:2016-11-11 05:00:00 +0000
2016-11-12 05:00:00 +0000
found 0 items using iterator
Is it possible to retrieve events from, say 2014? I see them when using the calendar app (macOS and iOS).
I think what happened is that the settings on my device changed during the iOS upgrade process (I know not how). In Settings/Calendar You can specify how events are synched. I found it was set to "1 month". I am now able to retrieve events older than 1 month.
The event store should return whatever matches the predicate you give it as long as you don't make the range too large (there IS a limit, I'd have to go look it up). In your example, you're only telling it to search within a range of 1 day. You're also narrowing it down to one specific calendar (you can pass nil to search all calendars). If you want events from 2014, then give it a range in 2014.

Resources