用目标C创建ICS

时间:2014-04-29 16:40:59

标签: objective-c

我正在为我大学的学生写一个应用程序。此时,我想将计划中的课程导出为ics文件。我已经写了一些与csv文件一起使用的东西,但现在我想要同样的ics。 问题是我的Mac上的iCal不想要这个代码中的应用程序创建的ics文件:

    -(NSURL*)getFileUrl
{
    NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [docsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Stundenplan-%@.ics", _MatrNr]];
    NSURL *fileUrl     = [NSURL fileURLWithPath:filePath];


    NSError *error = nil;
    [[self getICSData] writeToURL:fileUrl atomically:YES];
    if (error) {
        NSLog(@"Error while writing to File: %@", [error localizedDescription]);
    }

    return fileUrl;
}

-(NSData*)getICSData
{
    NSMutableString *erg = [[NSMutableString alloc] init];
    NSDate *aktDatum = [NSDate date];

    [erg appendString:[NSString stringWithFormat:@"BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//www.htw-dresden.de//iOS//DE\nMETHOD:PUBLISH\n"]];


    for (Stunde *this in _daten) {
        NSString *titel = [this.titel stringByReplacingOccurrencesOfString:@"," withString:@"\\, "];
        NSString *dozent = [this.dozent stringByReplacingOccurrencesOfString:@"," withString:@"\\, "];

        NSString *uuid = [[NSUUID UUID] UUIDString];

        [erg appendString:[NSString stringWithFormat:@"BEGIN:VEVENT\nUID:%@\n", uuid]];
        [erg appendString:[NSString stringWithFormat:@"DTSTART:%@T%@Z\n",[self nurTagFromDate:this.anfang], [self nurUhrzeigFromDate:this.anfang]]];
        [erg appendString:[NSString stringWithFormat:@"DTEND:%@T%@Z\n",[self nurTagFromDate:this.ende], [self nurUhrzeigFromDate:this.ende]]];
        [erg appendString:[NSString stringWithFormat:@"LAST-MODIFIED:%@T%@Z\nSEQUENCE:0\nSTATUS:CONFIRMED\n", [self nurTagFromDate:aktDatum], [self nurUhrzeigFromDate:aktDatum]]];
        [erg appendString:[NSString stringWithFormat:@"SUMMARY:%@\nDESCRIPTION:%@\nLOCATION:%@\nEND:VEVENT\n", titel, dozent, this.raum]];
    }

    [erg appendString:@"END:VCALENDER"];

    NSData *ret = [erg dataUsingEncoding:NSUTF8StringEncoding];

    return ret;
}

在getFileUrl中我想返回一个URL到那里创建的文件。这个函数调用通过我的数组(_daten)的getICSData函数,并为每个Stunde对象创建这个ics - " code"。 为了帮助我有这些NSDate格式化功能:

-(NSString*)nurTagFromDate:(NSDate*)date
{
    NSDateFormatter *nurTag = [[NSDateFormatter alloc] init];
    [nurTag setDateFormat:@"yyyyMMdd"];
    return [nurTag stringFromDate:date];
}

-(NSString*)nurUhrzeigFromDate:(NSDate*)date
{
    NSDateFormatter *nurTag = [[NSDateFormatter alloc] init];
    [nurTag setDateFormat:@"HHmmss"];
    return [nurTag stringFromDate:date];
}

作为输出我得到这样的东西:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//www.htw-dresden.de//iOS//DE
METHOD:PUBLISH
BEGIN:VEVENT
UID:CCCAC9B3-E056-47E3-A63B-F2FE2C3BA454
DTSTART:20140318T111000Z
DTEND:20140318T124000Z
LAST-MODIFIED:20140429T182723Z
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Internet-Technologien I
DESCRIPTION:Vogt\, J.
LOCATION:Z 254
END:VEVENT
.
.
.
END:VCALENDER

但我Mac上的日历应用不想打开文件......

如果你们中的一些人有一个想法会很棒:)

1 个答案:

答案 0 :(得分:5)

替换此行:

[erg appendString:@"END:VCALENDER"];

有了这个:

[erg appendString:@"END:VCALENDAR"];

注意VCALENDAR中的拼写错误。

您可能会发现此iCal file validator有用。