karthikeyan
karthikeyan

Reputation: 3888

Events not add into calender?

i am getting date and time from service,and i have merged both date and time as single,then i store it into event date but not added into calender. Here i have so far combining date and time

-(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;
}

store into 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 is my date value from service. timeToBeStore is my time value from service.

My server values eventDate = "12-09-2015"; eventTime = "13:45:00";

dateToBeStore=[NSString stringWithFormat:@"%@",[response valueForKey:@"eventDate"]];
    timeToBeStore=[NSString stringWithFormat:@"%@",[response valueForKey:@"eventTime"]];

What could be the reason?what was problem with this code? If i just give date alone,then working fine.

Upvotes: 0

Views: 113

Answers (2)

karthikeyan
karthikeyan

Reputation: 3888

Found a solution I need to set a end date for this

-(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.

The problem is: If u not set endDate,u will face this error Error Domain=EKErrorDomain Code=3 "No end date has been set." UserInfo=0x1753f660 {NSLocalizedDescription=No end date has been set.}

Upvotes: 0

Phani Sai
Phani Sai

Reputation: 1233

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
        }
    }

Upvotes: 1

Related Questions