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

女生的网名这么多〃 提交于 2020-01-02 07:10:29

问题


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

来源:https://stackoverflow.com/questions/18830805/how-can-i-have-uidocumentinteractioncontroller-show-calendar-as-an-option-for-op

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!