gpt4 book ai didi

objective-c - 以 Objective-C 创建 ICS

转载 作者:搜寻专家 更新时间:2023-10-30 20:02:34 24 4
gpt4 key购买 nike

我正在为我大学的学生编写一个应用程序。此时我想将时间表中的类(class)导出为 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 上的日历应用程序不想打开文件...

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

最佳答案

替换这一行:

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

有了这个:

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

注意 VCALENDAR 中的拼写错误。

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

关于objective-c - 以 Objective-C 创建 ICS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23370498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com