0

我正在使用以下代码将事件添加到我的日历中,但它没有显示任何错误,也没有保存到我的日历中。请帮忙。

EKEventStore *eventstore = [[EKEventStore alloc] init];
    if([eventstore respondsToSelector:@selector(requestAccessToEntityType:completion:)])    
        {
            [eventstore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
            {
                if(granted)
                {
                    // if user granted your app to use calendar control will reach over hare
                    // add the code to add event to iCal that you are using previously for iOS5

                    EKEvent *event  = [EKEvent eventWithEventStore: eventstore];
                    event.title = @"Title";
                    event.notes = @"Description";
                    event.location = @"Location";
                    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
                    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
                    [dateFormat setTimeZone:timeZone];
                    [dateFormat setDateFormat:@"dd-MM-yyyy hh:mm a"];
                    NSDate *date = [dateFormat dateFromString:[NSString stringWithFormat:@"%@",[[eventsArray objectAtIndex:index] objectForKey:@"EventDate"]]];
                    event.startDate =date;
                    event.endDate = [date dateByAddingTimeInterval:36000];;
                    event.availability = EKEventAvailabilityFree;

                    [event setCalendar:[eventstore defaultCalendarForNewEvents]];
                    NSError *err;
                    [eventstore saveEvent:event span:EKSpanThisEvent error:&err];

                }
                else
                {
                    NSLog(@"Calender not called");
                    // if user did not your app to use calendar control will reach over hare
                }
            }];
        }
4

2 回答 2

0

我有同样的问题,没有错误,但日历中没有事件。检查 iphone 设置:icloud:calendars 开关已打开。

于 2013-10-02T20:36:01.593 回答
0

首先我添加了eventkit框架,然后将#import EventKit/EventKit.h into <>添加到我的视图控制器中,然后实现了如下代码,它对我来说很好,希望你也可以通过参考这个找到解决方案:)

-(IBAction)AddEvent:(id)sender
{
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])            {
    // iOS 6 and later
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted){
            //---- codes here when user allow your app to access theirs' calendar.
            [self performCalendarActivity:eventStore];
        }else
        {
            //----- codes here when user NOT allow your app to access the calendar.
        }
    }];
}
else {
    //---- codes here for IOS < 6.0.
    [self performCalendarActivity:eventStore];
}
}

-(void)performCalendarActivity:(EKEventStore *)eventStore
{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];

if([txtTitle.text isEqualToString:@""] || txtTitle.text == NULL)
    txtTitle.text=@"Event Title";

event.title= txtTitle.text;

NSDate *duedate = pkrDate.date;
event.startDate =duedate;
event.endDate= [[NSDate alloc] initWithTimeInterval:600 sinceDate:duedate];

if(switchAlarm.on==TRUE){
    NSArray *arrAlarm = [NSArray arrayWithObject:[EKAlarm alarmWithAbsoluteDate:duedate]];
    event.alarms= arrAlarm;
}

[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
BOOL isSuceess=[eventStore saveEvent:event span:EKSpanThisEvent error:&err];

if(isSuceess){
    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Event" message:@"Event added in calendar" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertview show];
    [alertview release];
}
else{
    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Event" message:[err description] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertview show];
    [alertview release];
}
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
于 2013-08-22T12:33:51.297 回答