Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

objective c - URL scheme for opening native calendar with specific date

I have found the sample code to open calendar from my app, but i can't open at a specific date.

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

Is there a way to add specific date at the end of the "lunchUrl" string so when the user opens the calendar it displays the given date.

I have already tried the following formats: @"calshow://?=2013 12 19", @"calshow://?=2013-12-19", @"calshow://?=2013+12+19". None of these seem to work for me... any ideas what am i'm doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I played with this url scheme a little and found the way to do it. The main two points are:

  1. Don't use "//" after the calshow:
  2. Pass timestamp since reference date (1 Jan 2001)

Here is the code:

- (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];
}

And this is how I call it:

// 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]];
}

Perhaps you should take into account that calshow: is not a public url scheme, so maybe Apple would frown upon using it in this way. Or maybe they wouldn't (I haven't researched that).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...