1

I have used following code for creating calendar and adding event in that calendar for my application. Everything works fine in the simulator but the calendar is not created in the device. In privacy it does show that my application has the access of the user calendar.

Can anyone please let me know what am I missing?

Thanks in advance.

 // this is added in view did load for access
   [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

            // handle access here

        }]; 

//add event to main calendar

            EKEventStore *eventStore = [[EKEventStore alloc] init];

           // [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

                // handle access here

           // }];

            EKSource *localSource = nil;

            for (EKSource *source in eventStore.sources)

                if (source.sourceType == EKSourceTypeLocal)

                {
              localSource = source;
              break;

                }

            EKCalendar *cal;

          NSUserDefaults *calid = [NSUserDefaults standardUserDefaults];

            if(![calid valueForKey:@"calid"])

            {

            //cal = [EKCalendar calendarWithEventStore:eventStore];

            cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];

            cal.title = @"my calendar title";

            cal.source = localSource;

            [eventStore saveCalendar:cal commit:YES error:nil];

                NSString *strid= [NSString stringWithFormat:@"%@",cal.calendarIdentifier];

                [calid setObject:strid forKey:@"calid"];

            NSLog(@"cal id = %@", cal.calendarIdentifier);

            }

            EKEvent *event = [EKEvent eventWithEventStore:eventStore];

            event.title = myTextField.text;
            event.startDate = SelectedDate;

            event.endDate = [[NSDate alloc] initWithTimeInterval:3600 sinceDate:event.startDate];

            //[event setCalendar:[eventStore defaultCalendarForNewEvents]];

            [event setCalendar:[eventStore calendarWithIdentifier:[calid valueForKey:@"calid"]]];

           NSError *err;

            [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

            NSLog(@"%@",err);
4

0 回答 0