【问题标题】:Using UIDocumentInteractionController in simulator在模拟器中使用 UIDocumentInteractionController
【发布时间】:2012-12-04 11:31:14
【问题描述】:

我对@9​​87654321@ 感到非常绝望。我唯一想做的就是将我的应用程序中的 pdf 文件与注册 PDF 的所有其他应用程序共享。

这是我的代码:

NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
self.docController.delegate = self;

NSURL *url = [NSURL fileURLWithPath:filePath];
BOOL isValid = [[UIApplication sharedApplication] canOpenURL:url];
NSLog(@"uti: %@", [self.docController UTI]);

//[self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
[self.docController presentOpenInMenuFromBarButtonItem:self.backBut animated:YES];

isValid 变量始终为 TRUE,UTI 记录为 com.adobe.pdf(这似乎是正确的)但调用 presentOpenFrom... 两个调用(一个已注释)始终返回 NO。

我正在使用模拟器进行测试,是这个问题吗?是否有人发现代码有问题或知道要检查什么?

【问题讨论】:

    标签: ios ios-simulator share


    【解决方案1】:

    如果您正在处理 PDF 文件,那么您应该有任何能够在您的设备(模拟器)上打开 PDF 文件的应用程序。 您可能想要安装PDF File Viewer,因为它是开源的。 通过从 Xcode 运行安装应用程序后,您的 UIDocumentInteractionController 将正确显示。

    这是一个示例代码,用于从 Web 下载任何文档并显示 UIDocumentInteractionController 以将其传递给另一个应用程序。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    BOOL isDir = NO;
    NSError *error;
    if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir   == NO)
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
    }
    NSString *filePath = [cachePath stringByAppendingPathComponent:url.lastPathComponent];
    NSData *fileData = [NSData dataWithContentsOfURL:url];
    [fileData writeToFile:filePath atomically:YES];
    NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:filePathURL];
    [self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
    

    【讨论】:

      【解决方案2】:

      试试这个:

      NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"];
      if (path) {
          NSURL* url = [NSURL fileURLWithPath:path];
          UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:url];
          docController.delegate = self;
          [docController presentPreviewAnimated:YES];
      }
      

      【讨论】:

        【解决方案3】:

        如果此实施所需的只是 PDF 的预览,Kenney 的解决方案仍然适用。但是,它并没有解决presentOpenInMenuFromRect或presentOpenInMenuFromBarButtonItem的原始问题。这只会在预览中显示 pdf,而不是实际的“打开方式...”对话框。除了使用设备来调出 Open In 控制器之外,没有其他已知的解决方案。

        presentOpenInMenu... 方法检查系统是否有任何已注册的 PDF 阅读器下载。由于iOS模拟器没有提供,也没有办法添加,所以这个功能只能在设备上测试。

        【讨论】:

        • 有趣,从未真正尝试过。如果可行,那么获得一个开源的 pdf 阅读器应该相当简单。是否已确认?
        猜你喜欢
        • 2011-04-06
        • 2011-10-08
        • 1970-01-01
        • 2016-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-09
        相关资源
        最近更新 更多