How can I have UIDocumentInteractionController show Calendar as an option for opening a .ics file?
Asked Answered
G

0

4

I am intercepting a type of URL in the webview I use in my app in order to download the file that it links to instead of just trying to open it in the webview. The link is to a .ics file. So I download that file into the temporary directory and then bring up the file in a UIDocumentInteractionController instance, but Calendar is not shown as one of the apps to open that .ics file in, just Mail, DropBox and "Copy".

As you can see in the commented out line in the code below I've tried making the link open with webcal:// at the front instead of http://, and also manually setting the UIDocumentInteractionController's UTI to no avail. Is there some reason my local .ics file would not show Calendar as an option for opening it?

//Test for link to an ics file
if ([urlToLoad rangeOfString:@"GetSingleEventCalendarFile" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
    //urlToLoad = [urlToLoad stringByReplacingOccurrencesOfString:@"http" withString:@"webcal"];
    //NSData *contact = [NSData dataWithContentsOfURL:[NSURL URLWithString:webCalProtocol]];
    //return NO;
    NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlToLoad]];
    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *respData, NSError *error){
        NSString *urlString = [req.URL absoluteString];
        NSString *actIdAndRest = [urlString substringFromIndex:[urlString rangeOfString:@"="].location + 1];
        NSString *actId = [actIdAndRest substringToIndex:[actIdAndRest rangeOfString:@"&"].location];
        NSString *fileName = [[@"activity" stringByAppendingString:actId] stringByAppendingString:@".ics"];
        NSString * path = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
        NSError *errorC = nil;
        BOOL success = [respData writeToFile:path
                                     options:NSDataWritingFileProtectionComplete
                                       error:&errorC];

        if (success) {
            NSBundle * bundleContaining = [NSBundle bundleWithPath:NSTemporaryDirectory()];
            NSLog(@"%@", bundleContaining);
            NSURL *fileURL = [bundleContaining URLForResource:fileName withExtension:@"ics"];
            documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
            //documentController.UTI = @"ics";
            [documentController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES];
        } else {
            NSLog(@"fail: %@", errorC.description);
        }
    }];
}
Grote answered 16/9, 2013 at 14:42 Comment(3)
Though this is over a year old, it still appears to be an issue with the UIDocumentInteractionController. @Liam, any luck finding a working solution?Fancyfree
Asked my first question on Quora in over a year yesterday, and then get a response on this one... weird. Anyway, if I remember correctly (not likely), I just let the links to ics files be opened normally and the download happened as one would hope? Not sure, sorry!Grote
Haha, crazy. Well last I checked, clicking on a link to an actual ics file in UIWebView lets you subscribe to that calendar which doesn't really download the data, but that does achieve the end goal of adding events to calendar. Unfortunately for me, the schedule I'm working with is dynamically created based on a user's personal data, and no actual ics file resides on the web server. Thanks for the quick response anyhow!Fancyfree

© 2022 - 2024 — McMap. All rights reserved.