objective-c - 用于打开具有特定日期的 native 日历的 URL 方案

标签 objective-c cocoa-touch calendar icalendar

我找到了从我的应用程序打开日历的示例代码,但我无法在特定日期打开。

NSString* launchUrl = @"calshow://";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];

有没有办法在“lunchUrl”字符串的末尾添加特定日期,以便当用户打开日历时它会显示给定日期。

我已经尝试过以下格式:@"calshow://?=2013 12 19", @"calshow://?=2013-12-19", @"calshow://?=2013+12 +19”。这些似乎都不适合我……知道我做错了什么吗?

最佳答案

我试了一下这个 url 方案,找到了实现它的方法。主要有两点:

  1. 不要在calshow:
  2. 后使用“//”
  3. 自引用日期(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/

相关文章:

ios - 仅允许针对不同方案/目标的特定翻译

iphone - nsfetchedresultscontroller 实现令人困惑

ios - UITextView 样式在设置文本属性后被重置

iphone - UIView 动画中的键值观察

javascript - 您知道可以快速选择本周的某一天的日期选择器吗?

iOS使用socket连接发送数据

ios - 在用户输入凭据之前取消了 UIWebView 中的基本身份验证

iphone - 添加 subview 后 superview 和parentviewcontroller nil

php - 我的 CMS 中的单一/重复计划

android - 使用 android api 创建新的同步日历