选择日历以添加 iOS 事件

Posted

技术标签:

【中文标题】选择日历以添加 iOS 事件【英文标题】:Choosing Calendar to add an iOS event 【发布时间】:2016-11-16 09:23:08 【问题描述】:

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

例如在下面的捕获中,可以看到有一个 iphone 日历,然后可能创建了 1 个或多个 gmail

我要做的是你可以选择日历你要在其中添加事件,这个日历必须提前创建,而不是系统选择日历。

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

P.D:我开始使用 objective-c 和 ios 编写代码

添加我正在测试的事件的代码并且它有效:

- (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];

【问题讨论】:

【参考方案1】:

首先,感谢@NarendraPandey 和@nehamishra 提供的帮助

我已经给出了我的问题的解决方案,然后它会给出解决方案,以防有人可以为你服务。

我已经创建了一个方法来获取本地和gmail的可用日历,代码如下:

- (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;

然后显示日历列表以便用户可以选择一个并在那里输入事件,我发现我必须使用Action Sheet,虽然我已经看到它已被某些论坛 cmets 的 *** 弃用,所以我用UIAlertController,如下:

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");

[self downloadCalendario: cal]; 函数负责从 Web 服务下载一些事件并将它们添加到所选日历中。

导致选择日历的视图如下:

将事件添加到所选日历的代码是:

-(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];


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

【讨论】:

【参考方案2】:

通过创建新日历来试试这个。

 -(EKEvent*)createEvent:(EKEventStore*)eventStore
        EKEvent *event = [EKEvent eventWithEventStore:eventStore];
        event.title = @"New Event";

        event.startDate = [NSDate date];
        event.endDate =[NSDate date];

        event.location=@"Location";
        event.allDay = YES;
        event.notes =@"Event description";

        NSString* calendarName = @"Calendar";
        EKCalendar* calendar;
        EKSource* localSource;
        for (EKSource *source in eventStore.sources)
            if (source.sourceType == EKSourceTypeCalDAV &&
                [source.title isEqualToString:@"iCloud"])
                localSource = source;
                break;
            
        
        if (localSource == nil)
            for (EKSource *source in eventStore.sources)
                if (source.sourceType == EKSourceTypeLocal)
                    localSource = source;
                    break;
                
            
        
        calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
        calendar.source = localSource;
        calendar.title = calendarName;
        NSError* error;
        [eventStore saveCalendar:calendar commit:YES error:&error];
        return event;

希望它会奏效。 如果您有任何疑问,请告诉我。

【讨论】:

非常感谢我会试试这个看看它是否对我有用,我会告诉你它是否对我有用 我尝试了您的代码并且可以正常创建一个新日历,但我希望能够选择一个已经创建的日历并在该日历中添加事件,也许我对问题的描述是不清楚或引起混淆,现在我将对其进行编辑 是的,在第二个屏幕截图中您可以看到。您可以从可用日历中选择日历。 我已经试过你的代码,我没有任何窗口来选择日历,也许我没有放一些必要的代码。对不起,我对 iOS 编程很陌生。 好的,请在***.com/questions/40525194/…查看我的答案【参考方案3】:
-(void)addEventOnCalendar
    EKEventStore *store = [EKEventStore new];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) 
        if (!granted) 
            return;
        
        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = @"What ever You want to save as your event";
        event.startDate = selectedDate;

        event.endDate = selectedDate1;

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

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

【讨论】:

感谢您的回答

以上是关于选择日历以添加 iOS 事件的主要内容,如果未能解决你的问题,请参考以下文章

iOS - 以编程方式将我们应用程序的自定义 URI 添加到存储在日历中的事件中

在日历中添加事件

使用 PhoneGap/jQuery Mobile 以编程方式在 iPhone/Android 日历中添加事件?

添加拖动功能以使用CLNDR.js从日历中选择多个(范围)日期

使用 EventKit 添加事件和日历

fullcalendar 可以添加事件的--日历