我从服务中获取日期和时间,并且我将日期和时间合并为一个,然后将其存储到活动日期中,但未添加到日历中。
到目前为止,我已经结合了日期和时间
-(NSDate *)getDateFromService:(NSString *)str{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateFormat:@"dd-MM-yyyy"];
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:str];
// return dateFromString;
NSDate *date = dateFromString;
// Get the current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
// Split the date into components but only take the year, month and day and leave the rest behind
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
// Build the date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
// Convert the string time into an NSDate
NSDate *time = [formatter dateFromString:timeToBeStore];
// Split this one in components as well but take the time part this time
NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit ) fromDate:time];
// Do some merging between the two date components
dateComponents.hour = timeComponents.hour;
dateComponents.minute = timeComponents.minute;
// Extract the NSDate object again
NSDate *result = [calendar dateFromComponents:dateComponents];
// Check if this was what you where looking for
NSLog(@"%@",result);
return result;
}
存储到EventKit
-(void)addToCalenderEvent:(NSString *)eventTitle{
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = eventTitle;
// NSString *atTime=[NSString stringWithFormat:@"%@T%@",dateToBeStore,timeToBeStore];
NSDate *date = [self getDateFromService:dateToBeStore];
event.startDate = date; //today
// event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
self.savedEventId = event.eventIdentifier; //save the event id if you want to access this later
}];
}
dateToBeStore是我的服务日期值。
timeToBeStore是我的服务时间价值。
我的服务器值
eventDate =“ 12-09-2015”;
eventTime =“ 13:45:00”;
dateToBeStore=[NSString stringWithFormat:@"%@",[response valueForKey:@"eventDate"]];
timeToBeStore=[NSString stringWithFormat:@"%@",[response valueForKey:@"eventTime"]];
可能是什么原因?此代码有什么问题?
如果我只给个约会,那就可以了。
最佳答案
找到了解决方案
我需要为此设置结束日期
-(void)addToCalenderEvent:(NSString *)eventTitle{
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = eventTitle;
// NSString *atTime=[NSString stringWithFormat:@"%@T%@",dateToBeStore,timeToBeStore];
NSDate *date = [self getDateFromService:dateToBeStore];
event.startDate = date; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
self.savedEventId = event.eventIdentifier; //save the event id if you want to access this later
}];
}
I just uncomment the endDate code,then its working.
问题是:
如果您未设置endDate,则您将遇到此错误
错误域= EKErrorDomain代码= 3“尚未设置结束日期。” UserInfo = 0x1753f660 {NSLocalizedDescription =尚未设置结束日期。}