我正在为我大学的学生写一个应用程序。此时,我希望将时间表中的课程导出为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-“代码”。为了获得帮助,我使用了以下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的日历应用不想打开文件..。
如果你们中的一些人有一个想法,那就太好了:)
发布于 2014-04-29 08:54:29
替换这一行:
[erg appendString:@"END:VCALENDER"];
在这方面:
[erg appendString:@"END:VCALENDAR"];
注意VCALENDAR中的错误。
您可能会发现这个iCal file validator很有用。
https://stackoverflow.com/questions/23370498
复制相似问题