17

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?

Andras Hunor
  • 171
  • 1
  • 1
  • 5

4 Answers4

30

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).

Andris Zalitis
  • 2,369
  • 18
  • 18
  • Hi Andris, i have used above code, its working perfectly for past date. if i give future date it come back to current date. Can you please give some suggestion for opent the future date. – Surfer Oct 27 '14 at 05:36
  • is this possible to get date from above default calendar? – Alok Aug 29 '16 at 11:06
4

For react native with momentjs:

const testDate = moment('2020–04–01'),      // date is local time
  referenceDate = moment.utc('2001–01-01'), // reference date is utc
  seconds = testDate.unix() — referenceDate.unix();
Linking.openURL('calshow:' + seconds); //opens cal to April 1 2020

https://medium.com/@duhseekoh/need-to-open-the-os-calendar-at-a-specific-date-in-react-native-55f3a085cf8e

duhseekoh
  • 1,263
  • 10
  • 13
3

This works on ios 8 - just add seconds since 00:00 1 Jan 2001, so to open cal on 2 Jan 2001

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

I'm using RubyMotion, so my code looks something like this:

url = NSURL.URLWithString("calshow:#{my_date - Time.new(2001,1,1)}")
UIApplication.sharedApplication.openURL(url)
jjnevis
  • 2,672
  • 3
  • 22
  • 22
3

Swift 3

UIApplication.shared.openURL(URL(string: "calshow:\(date.timeIntervalSinceReferenceDate)")!)

Comments on whether Apple allows to use this would be appreciated!

Frederick Squid
  • 591
  • 4
  • 16