0

I want to show the MBProgressHUD object - progressHud during process auto add about 100 events to calendar. Here is codes when i touch on the button to do this task.

- (IBAction)add_event_for_test:(id)sender{
    self.progressHud = [[MBProgressHUD alloc] initWithView:self.view];
    self.progressHud.labelText = @"Adding some events to test";
    self.progressHud.detailsLabelText = @"Please wait...";
    [self.view addSubview:self.progressHud];
    [self.progressHud showWhileExecuting:@selector(add_more_event_execute:) onTarget:self withObject:nil animated:YES]; 
}

In the add_more_event_execute function below if i try to request access to entity type for EKEntityTypeEvent to add the new event to calendar in iOS6 then the progressHud shows and hides immediately. I need the progressHud is shown until all events add to calendar completely. The progressHud works well if i don't request Access and run it in iOS5.

- (void) add_more_event_execute: (id)object{

    @autoreleasepool {
        eventStore=[[EKEventStore alloc] init];
        if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {

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

                if (granted){
                // now add 100 event to calendar
                    int i = 0;
                    while (i < 100) {
                        EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore];
                        NSString *tmp1 = @"Title iOS6: ";
                        tmp1 = [tmp1 stringByAppendingString:[NSString stringWithFormat: @" %d",i]];
                        addEvent.title = tmp1;

                        NSCalendar *calendar = [NSCalendar currentCalendar];
                        NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];
                        [components setMonth:0];
                        [components setDay:i - 50]; //reset the other components
                        [components setYear:0]; //reset the other components
                        NSDate *dayi = [calendar dateByAddingComponents:components toDate:[NSDate date] options:0];
                        addEvent.startDate = dayi;
                        addEvent.endDate = [addEvent.startDate dateByAddingTimeInterval:600];
                        [addEvent setCalendar:[eventStore defaultCalendarForNewEvents]];

                        NSError *err;
                        [eventStore saveEvent:addEvent span:EKSpanThisEvent error:&err];
                        if (err == nil) {
                            NSString* str = [[NSString alloc] initWithFormat:@"%@", addEvent.eventIdentifier];
                            NSLog(@"String iOS6 %d: %@ ngay:%@ ",i, str,addEvent.startDate);
                        }
                        else {
                            NSLog(@"Error %@",err);
                        }
                        i++;
                    } 
                }
                else
                {
                    //----- codes here when user NOT allow your app to access the calendar.
                }

            }];

        }
    }
}

Please explain my issue and give me a solution. Thanks!

4

0 回答 0