如何让UIDocumentInteractionController显示Calendar作为打开.ics文件的选项?

时间:2013-09-16 14:42:34

标签: ios calendar icalendar uidocumentinteraction

我在我的应用程序中使用的webview中拦截了一种URL,以便下载它链接到的文件,而不是仅仅尝试在webview中打开它。该链接指向.ics文件。所以我将该文件下载到临时目录中,然后在UIDocumentInteractionController实例中显示该文件,但是Calendar不会显示为打开该.ics文件的应用程序之一,只是Mail,DropBox和“Copy”。

正如您在下面的代码中的注释行中所看到的,我尝试使用webcal://在前面而不是http://打开链接,并且还手动设置UIDocumentInteractionController的UTI无效。是否有某些原因我的本地.ics文件不会显示日历作为打开它的选项?

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

0 个答案:

没有答案