我从服务中获取日期和时间,并且我已将日期和时间合并为单个,然后我将其存储到事件日期但未添加到日历中。 到目前为止,我已将日期和时间结合起来
-(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&#34 ;; eventTime =" 13:45:00&#34 ;;
dateToBeStore=[NSString stringWithFormat:@"%@",[response valueForKey:@"eventDate"]];
timeToBeStore=[NSString stringWithFormat:@"%@",[response valueForKey:@"eventTime"]];
可能是什么原因?这段代码有什么问题? 如果我只是单独约会,那么工作正常。
答案 0 :(得分:1)
In swift use following code
func addBookingInformationToCalender()
{
let store = EKEventStore()
store.requestAccessToEntityType(EKEntityType.Event) {(granted, error) in
if !granted { return }
let event = EKEvent(eventStore: store)
event.title = "Tittle"
event.startDate = start NSDate
event.endDate =End NSDate
event.notes = "Event Notes"
let structuredLocation = EKStructuredLocation(title: "Location Name")
let location = CLLocation(latitude: Location.geoCode!.latitude, longitude: Location.geoCode!.longitude)
structuredLocation.geoLocation = location;
if #available(iOS 9.0, *) {
event.structuredLocation = structuredLocation
} else {
// Fallback on earlier versions
// event.location = selectedLocation.name!
event.setValue(structuredLocation, forKey: "structuredLocation")
}
event.calendar = store.defaultCalendarForNewEvents
do{
try store.saveEvent(event, span: EKSpan.ThisEvent, commit: true)
}catch{
Print("Got error while adding event")
}
// self.savedEventId = event.eventIdentifier //save event id to access `enter code here`this particular event later
}
}
答案 1 :(得分:1)
找到解决方案 我需要为此
设置结束日期-(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 =未设置结束日期。}