首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用目标C创建ICS

用目标C创建ICS
EN

Stack Overflow用户
提问于 2014-04-29 16:40:59
回答 1查看 783关注 0票数 3

我正在为我大学的学生写一个应用程序。此时,我希望将时间表中的课程导出为ics-文件。我已经写了一些与csv有关的东西--文件,但现在我想要同样的东西用于ics。问题是,我的Mac上的iCal不希望从下面的代码中创建应用程序创建的ics-文件:

代码语言:javascript
运行
复制
    -(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-“代码”。为了获得帮助,我使用了以下NSDate格式函数:

代码语言:javascript
运行
复制
-(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];
}

作为输出,我得到如下内容:

代码语言:javascript
运行
复制
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的日历应用不想打开文件..。

如果你们中的一些人有一个想法,那就太好了:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-29 16:54:29

替换这一行:

代码语言:javascript
运行
复制
[erg appendString:@"END:VCALENDER"];

在这方面:

代码语言:javascript
运行
复制
[erg appendString:@"END:VCALENDAR"];

注意VCALENDAR中的错误。

您可能会发现这个iCal file validator很有用。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23370498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档