简体   繁体   English

在xcode iOS中将日历添加到日历

[英]Add Event to calendar in xcode iOS

Hy 海兰

I have this code for adding Events to calendar but it does not add. 我有这个代码用于向日历添加事件,但它没有添加。

-(void)event
{
    EKEventStore *eventStore = [[EKEventStore alloc] init];

    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    event.title     = @"Event";


    NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
    [tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];


    NSString *dateandtime =[NSString stringWithFormat:@"%@%@%@",datestring,@" ",starttimestring];
    NSString *dateandtimeend =[NSString stringWithFormat:@"%@%@%@",datestring,@" ",endtimestring];



    event.startDate = [tempFormatter dateFromString:dateandtime];
    event.endDate = [tempFormatter dateFromString:dateandtimeend];


    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];

    [event setCalendar:[eventStore defaultCalendarForNewEvents]];
    NSError *err;
    [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}

From the XML I get the date and time in this format: 从XML我得到这种格式的日期和时间:

datestring: 28.10.2012 日期字符串:28.10.2012

starttimestring: 15:00 starttimestring:15:00

Are you on the iOS 6 simulator or on a device with iOS 6? 您是iOS 6模拟器还是iOS 6设备? If so, you need to ask the user for permission to use the event store before you can save items to it. 如果是这样,您需要在用户保存项目之前询问用户是否允许使用事件存储。

Basically, if the requestAccessToEntityType:completion: selector is available on your event store object, you call that method and provide a block of code that is executed when the user grants permission, and you would then do your event saving in that block. 基本上,如果事件存储对象上有requestAccessToEntityType:completion:选择器,则调用该方法并提供在用户授予权限时执行的代码块,然后您将在该块中执行事件保存。

First add the EventKit framework to your project and don't forget to include the import: 首先将EventKit框架添加到您的项目中,不要忘记包含导入:

#import <EventKit/EventKit.h>

Here is a code snippet that I used that worked for me: 这是我使用的代码片段,对我有用:

EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
    // the selector is available, so we must be on iOS 6 or newer
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (error)
            {
                // display error message here
            }
            else if (!granted)
            {
                // display access denied error message here
            }
            else
            {
                // access granted
                // ***** do the important stuff here *****
            }
        });
    }];
}
else
{
    // this code runs in iOS 4 or iOS 5
    // ***** do the important stuff here *****
}

[eventStore release];

Here is a blog post that I did on this subject: 这是我在这个主题上做过的博客文章:

http://www.dosomethinghere.com/2012/10/08/ios-6-calendar-and-address-book-issues/ http://www.dosomethinghere.com/2012/10/08/ios-6-calendar-and-address-book-issues/

1) add Eventkit framework and #import <EventKit/EventKit.h> 1)添加Eventkit框架和#import <EventKit/EventKit.h>

2) 2)

 -(void)syncWithCalendar {
    EKEventStore *store = [EKEventStore new];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) { return; }
        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = @"Event Title Testing"; //give event title you want
        event.startDate = [NSDate date];
        event.endDate = [event.startDate dateByAddingTimeInterval:60*60];
        event.calendar = [store defaultCalendarForNewEvents];
        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
    }];
}

3) call function 3)通话功能

[self syncWithCalendar];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM