[英]Issue with Apple's EKEvent Sample
我正在尝试从用户的日历应用中获取事件。 这本身很好,但有一点不太对。 我在不同的标签中显示title
, location
, startDate
和endDate
。 现在,我的问题是endDate
和startDate
都运行了一个小时,所以当我将事件设置为19:00时,它在我的应用程序中显示为18:00。 我正在使用此代码:
// Create the predicate's start and end dates.
CFGregorianDate gregorianStartDate, gregorianEndDate;
CFGregorianUnits startUnits = {0, 0, -30, 0, 0, 0};
CFGregorianUnits endUnits = {0, 0, 15, 0, 0, 0};
CFTimeZoneRef timeZone = CFTimeZoneCopySystem();
gregorianStartDate = CFAbsoluteTimeGetGregorianDate(
CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(), timeZone, startUnits),
timeZone);
gregorianStartDate.hour = 0;
gregorianStartDate.minute = 0;
gregorianStartDate.second = 0;
gregorianEndDate = CFAbsoluteTimeGetGregorianDate(
CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(), timeZone, endUnits),
timeZone);
gregorianEndDate.hour = 0;
gregorianEndDate.minute = 0;
gregorianEndDate.second = 0;
NSDate* startDate =
[NSDate dateWithTimeIntervalSinceReferenceDate:CFGregorianDateGetAbsoluteTime(gregorianStartDate, timeZone)];
NSDate* endDate =
[NSDate dateWithTimeIntervalSinceReferenceDate:CFGregorianDateGetAbsoluteTime(gregorianEndDate, timeZone)];
CFRelease(timeZone);
// Create the predicate.
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil]; // eventStore is an instance variable.
// Fetch all events that match the predicate.
NSArray *events = [eventStore eventsMatchingPredicate:predicate];
有人知道如何解决这个问题吗? 提前致谢!
好吧,自己想通了。 我要做的就是添加这一行:
int z = [[NSTimeZone localTimeZone] secondsFromGMT] / 3600;
并将z添加到startDate
和endDate
您是肯定的,系统的时区与日历的时区相同。 我之前遇到过一些奇怪的问题(一两个小时),因为我和创建日历事件的时区不同。 奇怪的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.