如何在app ios中添加添加事件选项来添加自定义日历?

时间:2014-03-11 06:26:51

标签: ios iphone objective-c calendar

如何在app ios中添加活动添加自定义日历?是否可以添加?请帮我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

 First Add EventKit Framework.

 Second Add #import <EventKit/EventKit.h>

并且您可以使用以下代码在您的应用中添加事件数据:

// Create Event
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];


NSDateFormatter *dateFormats = [[NSDateFormatter alloc]init];
[dateFormats setDateFormat:@"yyy-MM-dd HH:mm:ss"];

NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] init];
date1 = nil;
date2 = nil;
date1 = [dateFormats dateFromString:startTime];
date2 = [dateFormats dateFromString:endTime];

event.title  = @"Test Event";
event.allDay = NO;
event.startDate = date1;
event.endDate = date2;
event.notes = @"Nice Event on Tuesday";
event.location = @"NewYork, USA";

//put here if start and end dates are same
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;

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

if(err == noErr)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        // Display/dismiss your alert

        NSString *title = nil;
        NSString *description  = nil;
        NSString *cancelButton = nil;

        title = @"Event added - successfully!";
        description = nil;
        cancelButton = @"OK";

        UIAlertView *alerts = [[UIAlertView alloc] initWithTitle:title
                                                         message:nil
                                                        delegate:nil
                                               cancelButtonTitle:cancelButton
                                               otherButtonTitles:nil];
        [alerts show];
        [alerts release];
    });
}