0

我已使用此代码将事件添加到 iphone 日历

-(void)setEvent:(NSString *)title withStartDate:(NSDate *)startDate withEndDate:(NSDate *)endDate completion:(void (^)(void))completionBlock
{

    EKEventStore* store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted)
        {
            return;
        }
        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = title;
        event.startDate = startDate;
        event.endDate = endDate;
        [event setCalendar:[store defaultCalendarForNewEvents]];
        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
       // NSString *savedEventId = event.eventIdentifier;
        NSString* alertTitle;
        NSString* msg;
        if (err) {

            alertTitle = @"Calendar was not set";
            msg = @"Please set default calendar in settings.";
        }
        else
        {
            alertTitle = @"Event added";
            msg = @"Event had been added in your calendar.";
        }
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:alertTitle message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        completionBlock();

    }];
}

但是添加事件非常慢。我必须等待大约 10-12 秒才能完成此方法。这是正常的还是我做错了什么?我已经搜索过相同的内容,但仍然没有运气。任何建议将不胜感激。

4

3 回答 3

8

经过两天的研究,我找到了解决方案——我把事件保存代码放在dispatch_async这里是我添加事件的更新方法——

-(void)setEvent:(NSString *)title withStartDate:(NSDate *)startDate withEndDate:(NSDate *)endDate withResecheduling:(BOOL)rescheduling completion:(void (^)(void))completionBlock
{

    [[BusyIndicator sharedInstance] startIndicator];
    EKEventStore* store = [[EKEventStore alloc] init];

    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted)
        {
            return;
        }
                dispatch_async(dispatch_get_main_queue(), ^{
                    EKEvent *event = [EKEvent eventWithEventStore:store];
                    event.title = title;
                    event.startDate = startDate;
                    event.endDate = endDate;
                    [event setCalendar:[store defaultCalendarForNewEvents]];
                    NSError *err = nil;
                    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
                    // NSString *savedEventId = event.eventIdentifier;
                    if (!rescheduling) {

                        NSString* alertTitle;
                        NSString* msg;
                        if (err) {

                            alertTitle = @"Calendar was not set";
                            msg = @"Please set default calendar in settings.";
                        }
                        else
                        {
                            alertTitle = @"Event added";
                            msg = @"Event has been added in your calendar.";
                        }
                        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:alertTitle message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                        [alert show];
                    }

                    [[BusyIndicator sharedInstance] stopIndicator];
                    completionBlock();
        });


    }];
}
于 2013-10-17T09:01:28.707 回答
1

请试试这个......

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

if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
    // iOS 6 and later
    // This line asks user's permission to access his calendar
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
     {
         if (granted) // user user is ok with it
         {
             EKEvent *event = [EKEvent eventWithEventStore:eventStore];
             event.title  = eventname;
             event.allDay = NO;                 

             event.startDate = StartDateobject; // StartDateObject is Object of Date Class
             event.endDate=event.startDate;

             [event setCalendar:[eventStore defaultCalendarForNewEvents]];
             NSError *err;

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

             if(err)
             {

                 NSLog(@"Unable to save event to the calendar!: Error= %@", err);

             }
             else
             {

                 NSLog(@"Event added successfully.");

             }
         }

     }];
}

// iOS < 6
else
{
    EKEvent *event = [EKEvent eventWithEventStore:eventStore];
    event.title  = eventname;
    event.allDay = NO;

    event.startDate = StartDateobject; // StartDateObject is Object of Date Class
    event.endDate=event.startDate;


    [event setCalendar:[eventStore defaultCalendarForNewEvents]];
    NSError *err;

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

    if(err)
    {

         NSLog(@"unable to save event to the calendar!: Error= %@", err);

    }
    else
    {

        NSLog(@"Event added successfully..");

    }
}
于 2013-10-15T11:35:54.727 回答
1

尝试更换:

  [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

经过

  [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
于 2013-10-15T10:30:07.527 回答