Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opening an ics file from document directory in UIWebView or in Calendar App

My iPhone app (supporting iOS 6+) has a web view which lists events with a "Add To Calendar" button for each event. The "Add To Calendar" button is a hyperlink to an .ics file on the server. I have modified IIS to use "text/calendar" as the MIME type for .ics files and used the following code in my UIWebView delegate's shouldStartLoadWithRequest: when the request URL contains the ".ics" path extension

[[UIApplication sharedApplication] openURL:ics_url];

When tapping on the "Add To Calendar" button for an event, I get "Cannot subscribe to !event name! from !environment path!." However, I can open the file in the device's safari browser.

So I tried a few things none of which worked

1) Loaded file contents to NSData and tried to load data in webview by using

tried this -

[mywebview loadData:webData MIMEType:@"text/calendar" textEncodingName:@"utf-8" baseURL:RequestURL];

and this -

[myWebView loadData:webData MIMEType:@"text/calendar" textEncodingName:@"utf-8" baseURL:nil];

-- I am able to see the file contents when I NSLog it. But webview:didFailLoadWithError gets called and nothing loads. The webview loads the text of the vcalendar file if I change the MIME type to text/html.

2) Downloaded the file contents and saved it to document directory and tried to open the file

[webDataString writeToFile:pathToFileInDocumentDirectory atomically:NO encoding:4 error:&error];
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:pathToFileInDocumentDirectory]]];

-- again, the file contents are saved properly. I am able to reload them and view the contents. But web view doesn't load the request.

3) I even tried this which failed at canOpenUrl (as expected)

if ([[UIApplication sharedApplication] canOpenURL:[NSURL fileURLWithPath:pathToFileInDocumentDirectory]]) {
                    [[UIApplication sharedApplication] openURL:[NSURL fileURLWithPath:pathToFileInDocumentDirectory]];
                }

HELP!!!

like image 225
Dave Avatar asked Jan 31 '26 13:01

Dave


1 Answers

The problems seemed to be a combination of webcal (had to use https, instead) protocol and the way MIME type setting was done on IIS. So luckily, I didn't have to explore the other ways of achieving this.

[[UIApplication sharedApplication] openURL:ics_url]; 

The above seemed to work.

like image 59
Dave Avatar answered Feb 02 '26 04:02

Dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!