3

我在我的应用程序中使用的 webview 中截取了一种 URL,以便下载它链接到的文件,而不是仅仅试图在 webview 中打开它。该链接指向 .ics 文件。因此,我将该文件下载到临时目录中,然后在 UIDocumentInteractionController 实例中显示该文件,但日历并未显示为打开该 .ics 文件的应用程序之一,只有 Mail、DropBox 和“复制”。

正如您在下面代码中注释掉的行中看到的那样,我尝试在前面使用 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);
        }
    }];
}
4

0 回答 0