选择日历以添加iOS事件 [英] Choosing Calendar to add an iOS event

查看:70
本文介绍了选择日历以添加iOS事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将事件添加到iOS设备中的日历中,现在我必须将事件添加到 defaultCalendarForNewEvents 日历中,但是我想要的是以便能够选择已经创建了要在其中添加事件的日历。



例如,在下面的截图中,可以看到有一个iphone日历,然后可能会创建1个或几个gmail





我要做的是您可以选择要在其中添加事件的日历,该日历必须更早创建,而不是系统选择日历。



有什么建议吗?有示例吗?



PD:我开始使用



以及将事件添加到的代码所选日历为:

 -(void)addEventOnCalendar:(EKCalendar *)cal {

EKEventStore * store = [EKEventStore新];
[存储requestAccessToEntityType:EKEntityTypeEvent完成:^(已授予布尔值,NSError *错误){
如果(已授予){
返回;
}
EKEvent * event = [EKEvent eventWithEventStore:store];
event.title = @测试;

NSDate * cDate = [NSDate日期];
event.startDate = [cDate dateByAddingTimeInterval:((5 * 60 * 60)+(30 * 60)+ 15)];
event.endDate = [cDate dateByAddingTimeInterval:((5 * 60 * 60)+(30 * 60)+ 15)];

//event.calendar = [store defaultCalendarForNewEvents];
event.calendar = [存储calendarWithIdentifier:cal.calendarIdentifier];

NSError * err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:& err];

}];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@消息消息:@事件已成功添加委托:self cancelButtonTitle:@确定 otherButtonTitles:无,无];
[警报显示];

}

我希望它能对某人有所帮助。


I'm trying to add events to the calendar in iOS devices, for now I've got to add an event to the defaultCalendarForNewEvents calendar, but what I want is to be able to choose a calendar that has already been created in which I want to add the event.

For example in the following capture it is seen that there is an iphone calendar and then there may be 1 or several gmail created

What I want to do is that you can choose the calendar in which you want to add the event, this calendar must be created earlier, not that the system chooses the calendar.

Any recommendations? Any examples?

P.D: I'm starting to code with and

Code to add events I'm testing and it works:

- (void) addEventCalendar: (Evento_DTO *) evento {
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEvent *event = [EKEvent eventWithEventStore:eventStore];
    EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];


    event.title = @"Test Event";
    reminder.title = @"Test reminder";

    NSDate *cDate = [NSDate date];
    NSLog(@"current date  %@", cDate);


    /*NSDateComponents *startDateComponents = [[[NSDateComponents alloc] init] autorelease];
     [startDateComponents setDay:12];
     [startDateComponents setMonth:12];
     [startDateComponents setYear:2012];
     [startDateComponents setHour:12];
     [startDateComponents setMinute:18];


     NSDateComponents *endDateComponents = [[[NSDateComponents alloc] init] autorelease];
     [endDateComponents setDay:12];
     [endDateComponents setMonth:12];
     [endDateComponents setYear:2012];
     [endDateComponents setHour:12];
     [endDateComponents setMinute:18];
     [endDateComponents setSecond:20];*/


    //event.startDate = cDate;
    //event.endDate = [cDate dateByAddingTimeInterval:15.0];

    event.startDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 15)];
    event.endDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 30)];


    //event.startDate = [[NSCalendar currentCalendar] dateFromComponents:startDateComponents];
    //event.endDate = [[NSCalendar currentCalendar] dateFromComponents:endDateComponents];

    reminder.completionDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 10)];

    NSLog(@"startdate  %@", event.startDate);
    NSLog(@"enddate  %@", event.endDate);


    [event setCalendar:[eventStore defaultCalendarForNewEvents]];

    //[reminder setCalendar:[eventStore defaultCalendarForNewReminders]];

    NSError *error = nil;

    [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
    //[eventStore saveReminder:reminder commit:YES error:&error];
}

解决方案

First of all, thank @NarendraPandey and @nehamishra for the help provided

I have given the solution to my problem, which will then put the solution in case someone can serve you.

I have created a method to obtain the available calendars that are local and those of gmail, the code is the following one:

- (NSMutableArray*) getCalendars {

    NSMutableArray *res =[[NSMutableArray alloc] init];

    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEntityType type = EKEntityTypeEvent;
    NSArray *calendars = [eventStore calendarsForEntityType: type];

    for ( EKCalendar *cal in calendars )
    {
        if (cal.type == EKCalendarTypeCalDAV || cal.type == EKCalendarTypeLocal  ){
            NSLog(@"cal nombre:- %@ ", cal.title);
            [res addObject: cal];

        }
    }

    return res;
}

Then to show the list of calendars so that the user can select one and enter the events there, I found that Ihave to use an Action Sheet, although I have seen that it was deprecated according to some forum comments Of StackOverflow, so I used UIAlertController, being as follows:

NSMutableArray* cals =  [self getCalendars];

if([cals count] > 0){//Comprobamos que existan calendarios

   UIAlertController *alert = [UIAlertController   alertControllerWithTitle:AMLocalizedString(@"calendar_dialog_info", @"")
          message:nil
          preferredStyle:UIAlertControllerStyleActionSheet];

   for ( EKCalendar *cal in cals )
   {
         UIAlertAction *calAction = [UIAlertAction actionWithTitle: cal.title
         style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {

         NSLog(@"You pressed button %@ ", cal.title);

         [self descargarCalendario:  cal];
          }];

          [alert addAction:calAction];
   }

   UIAlertAction* cancel = [UIAlertAction
                                         actionWithTitle:AMLocalizedString(@"cancelar", @"")
               style:UIAlertActionStyleCancel
               handler:^(UIAlertAction * action)
               {
                    [alert dismissViewControllerAnimated:YES completion:nil];

               }];

         [alert addAction:cancel];

         [self presentViewController:alert animated:YES completion:nil];
}else{
    NSLog(@"No hay calendarios");
}

Where the [self downloadCalendario: cal]; function is responsible for downloading some events from a web service and adding them to the chosen calendar.

Resulting in the following view to choose the calendar:

And the code to add the event to the selected calendar is:

-(void)addEventOnCalendar: (EKCalendar *) cal{

    EKEventStore *store = [EKEventStore new];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) {
            return;
        }
        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = @"Test";

        NSDate *cDate = [NSDate date];
        event.startDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 15)];
        event.endDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 15)];

        //event.calendar = [store defaultCalendarForNewEvents];
        event.calendar = [store calendarWithIdentifier: cal.calendarIdentifier];

        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

    }];
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:@"Event Successfully added " delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [alert show];

}

I hope it helps somebody.

这篇关于选择日历以添加iOS事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆