5

I am trying to download and open .ics file in my app.

I found few question, and here's some code I am using

// NSString *path = [[NSBundle mainBundle] pathForResource:@"http://www.nmsd.wednet.edu//site/handlers/icalfeed.ashx?MIID=607" ofType:@"ics"];
NSURL *url = [NSURL fileURLWithPath:@"http://www.nmsd.wednet.edu//site/handlers/icalfeed.ashx?MIID=607"];
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:url];
dc.delegate = self;
[dc presentPreviewAnimated:YES];

Nothing at all happens. no errors at all. Would it be easier to look for a library to achieve this.

  • FYI - your http URL is not a file URL. `UIDocumentInteractionController` is meant to be used with local files, not remote files. – rmaddy Jun 19 '14 at 22:07
  • What do you actually want to happen after the ics file is downloaded? – rmaddy Jun 19 '14 at 22:08
  • @rmaddy Yes I tried both, I want to implement a calendar feature in my app. I am trying to use these link to maybe populate the native calendar. I have no clue to be honest, am I on the right path? please advise – WarehouseMuzik Jun 20 '14 at 14:57

3 Answers3

8

If you have a link to remote .ics file, then prepare it for Safari.

Let's suppose that you have a link to iCal file:

"pl-dev2.office.org/events/sync.ics?t=90613b6c7f8". 

Just add prefix webcal:// to your link.

Swift

let link = "webcal://pl-dev2.office/events/sync.ics?t=90613b6c7f8"

let webcal = NSURL(string: link)
UIApplication.sharedApplication().openURL(webcal)

Objective-C

NSString *link = "webcal://pl-dev2.office/events/sync.ics?t=90613b6c7f8"

NSUrl *webcal = [NSURL URLWithString:link];
[[UIApplication sharedApplication] openURL:webcal];

Safari will sync it in your Calendar app for You.

Daniel Krom
  • 9,751
  • 3
  • 43
  • 44
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • how can an http url be changed to webcal:// ?? – Deepak Thakur Jun 17 '15 at 06:57
  • calshow:// is used to open calendar from app [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"calshow://"]]; – Deepak Thakur Jun 17 '15 at 06:59
  • and there seems to be no other way to open the calendar app with the required ics file.. Do let me know how to open the ics file which i download from app / UIWebView and display the calendar app showing contents of the ics file downloaded.. – Deepak Thakur Jun 17 '15 at 07:01
  • 1
    That works, but it uses the URL as the calendar name. Is there any query param I can add to the webcal url to pass a custom calendar name or does that have to come from the ics file itself? – SudoPlz Jun 15 '20 at 17:41
  • 1
    But this asks you to add a calendar, but how can you add an event in your current calendar from the .ics file? (to do in swift code the same thing that Safari does when you open an URL that points to the .ics file ) – Andrei F Sep 15 '20 at 13:30
  • @AndreiF you could open the url with SFSafariController, you can check out my answer – 123 superatom Apr 16 '21 at 09:57
1

The @Bartłomiej Semańczyk is adding a calendar, if you want to add a event you can open url with SFSafariController

let link = "https://www.apple.com/v/apple-events/home/p/built/assets/event.ics?d=3213214324313"
let url = URL(string: link)!
let safariVC = SFSafariViewController(url: url)
present(safariVC, animated: true, completion: nil)
  • This is the only solution that worked for me. I just need to figure out how to "auto" dismiss the SafariVC after the Calendar Event gets added – carlachip Aug 11 '22 at 03:53
0

You use NSURL fileURLWithPath with an URL string as parameter although it expects file path. Change it to NSURL URLWithString.

zholobov
  • 11
  • 2