我如何使用iPhone日历添加事件?

时间:2012-05-08 16:10:49

标签: iphone titanium titanium-mobile

我想用iPhone日历添加活动。

我从github尝试ti.com.calendar模块但在此模块中只保存startTime EndTime,Title和details。 但是,不要使用allDay重复或不提醒。

我是如何使用它的。在日历?

我还提醒用户通知。但是,删除事件后。通知不是删除。

任何建议表示赞赏

1 个答案:

答案 0 :(得分:-1)

下面是代码的和平。警报日期用于匹配工作时间(用户不喜欢醒来做工作:)。

eventStore = [[EKEventStore alloc] init];

    EKEvent *newEvent = [EKEvent eventWithEventStore:eventStore];
    newEvent.calendar = eventStore.defaultCalendarForNewEvents;
    NSString *titleForEvent = [NSString stringWithFormat:@"In country:%@ will be:\n%@ event",[mo valueForKey:@"name"],[mo valueForKey:@"necessaryData"]];
    newEvent.title = titleForEvent;
    newEvent.allDay = YES;

    NSDate *date = [mo valueForKey:@"date"];
    NSDate *dateAlarm = [mo valueForKey:@"dateAlarm"];
    EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:[dateAlarm timeIntervalSinceDate:date]];

    if (dateAlarm < [NSDate date]){

        dateAlarm = [NSDate dateWithTimeIntervalSinceNow:+18000];
        NSDateFormatter *dateForm = [[NSDateFormatter alloc]init];
        [dateForm setDateFormat:@"%HH"];
        NSString *hourOfAlarm = [dateForm stringFromDate:dateAlarm];
        [dateForm release];
        NSNumberFormatter *numberForm = [[NSNumberFormatter alloc] init];
        NSNumber *hour = [numberForm numberFromString:hourOfAlarm];
        [numberForm release];
        int difference = 0;
        if ([hour intValue] < 9) difference = (9 - [hour intValue]) *3600;
        if ([hour intValue] > 17) difference = (17 - [hour intValue]) *3600;
        if (difference != 0) {
            NSTimeInterval interval = 18000 + difference;
            dateAlarm = [NSDate dateWithTimeIntervalSinceNow:interval];
        }
        alarm = [EKAlarm alarmWithRelativeOffset:[dateAlarm timeIntervalSinceDate:date]];
    }   
    newEvent.startDate = date;
    newEvent.endDate = date;

    //EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:[dateAlarm timeIntervalSinceDate:date]];
    newEvent.alarms = [NSArray arrayWithObject:alarm];

    NSError *error;

    BOOL saved = [eventStore saveEvent:newEvent span:EKSpanThisEvent error:&error];
    if (!saved && error) {
        NSLog(@"%@",[error localizedDescription]);
    } else [mo setValue:newEvent.eventIdentifier forKey:@"eventIdentifier"];