【发布时间】:2014-07-30 21:25:00
【问题描述】:
我正在为我大学的学生编写一个应用程序。此时,我想将日程中的课程导出为 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。此函数调用 getICSData 函数,该函数通过我的数组 (_daten) 并为每个 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 上的日历应用不想打开文件...
如果你们中的一些人有一个想法,那就太好了:)
【问题讨论】:
标签: objective-c