我找到了从我的应用程序打开日历的示例代码,但我无法在特定日期打开。
NSString* launchUrl = @"calshow://";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
有没有办法在“lunchUrl”字符串的末尾添加特定日期,以便当用户打开日历时它会显示给定日期。
我已经尝试过以下格式:@"calshow://?=2013 12 19", @"calshow://?=2013-12-19", @"calshow://?=2013+12 +19”。这些似乎都不适合我……知道我做错了什么吗?
最佳答案
我试了一下这个 url 方案,找到了实现它的方法。主要有两点:
- 不要在
calshow:
后使用“//”
- 自引用日期(2001 年 1 月 1 日)起传递时间戳
代码如下:
- (void)showCalendarOnDate:(NSDate *)date
{
// calc time interval since 1 January 2001, GMT
NSInteger interval = [date timeIntervalSinceReferenceDate];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"calshow:%ld", interval]];
[[UIApplication sharedApplication] openURL:url];
}
我是这样调用它的:
// create some date and show the calendar with it
- (IBAction)showCalendar:(id)sender
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:4];
[comps setMonth:7];
[comps setYear:2010];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[self showCalendarOnDate:[cal dateFromComponents:comps]];
}
也许您应该考虑到 calshow:
不是公共(public) url 方案,因此 Apple 可能不赞成以这种方式使用它。或者也许他们不会(我还没有研究过)。
关于objective-c - 用于打开具有特定日期的 native 日历的 URL 方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20684038/