【问题标题】:File sharing / send file in another app ("open in" in iOS)文件共享/在另一个应用程序中发送文件(在 iOS 中“打开”)
【发布时间】:2013-02-28 05:22:07
【问题描述】:

我的应用创建了一个名为 log.txt 的简单文件

这个文件的 URL(在 xcode 中查看)是 file://localhost/var/mobile/Applications/NUMBER OF THE APPLICATION/Documents/log.txt

所以我可以在 finder 中看到这个文件...

我想在我的应用程序中添加“打开方式”功能,以便用户共享此文件(通过邮件或 imessage)或在另一个兼容的应用程序中打开此文件。

这是我的工作:

-(void) openDocumentIn {

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:docFile]; //docFile is the path
//NSLog(@"%@",fileURL); // -> shows the URL in the xcode log window

UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
documentController.delegate = self;
documentController.UTI = @"public.text";
[documentController presentOpenInMenuFromRect:CGRectZero
                                       inView:self.view
                                     animated:YES];
}

然后调用这个函数:

-(IBAction)share:(id)sender {
    [self openDocumentIn];
}

当我运行应用程序时,我点击了这个“分享”按钮,但除了在日志窗口中显示 URL 的路径外,什么都没有附加...

我错过了一些东西......

谢谢

编辑:最后,它可以在我真正的 iphone 上运行……模拟器中没有文本查看器!!! --'

编辑 2:它显示了可用的应用程序(页面、凹凸...)但最终崩溃:(((!see here for the crash picture

【问题讨论】:

  • 您确定您的设备上安装了可以打开该文件类型的应用程序吗?
  • 在我的帖子中查看编辑和编辑 2
  • 打开断点导航器,点击右下角的+号,添加异常断点,完成。再次运行应用程序,现在调试器将在抛出异常时停止。
  • 2013-03-12 17:27:00.545 newapp[1930:907] -[__NSCFType _openDocumentWithApplication:]: unrecognized selector sent to instance 0x1dd7e220 这是否意味着我的 txt 文件无法通过页面打开? ??

标签: iphone ios xcode file share


【解决方案1】:

这对我来说是这样的:

我只是将声明“UIDocumentInteractionController *documentController”放在 .h 文件中,它就可以工作了!

我真的不知道为什么,但是....

【讨论】:

    【解决方案2】:

    这是一个内存管理问题。它崩溃的主要原因是对象没有被保留。这就是为什么如果你在 .h 文件中声明它并在你分配它时为保留写一个 @property 对象会被保留。

    所以在你的接口文件(.h)中你应该有

    @property (retain)UIDocumentInteractionController *documentController;
    

    然后在你的 .m(实现文件)中你可以做

    @synthesize documentController;
    
    - (void)openDocumentIn{
    
        // Some code here
    
        self.documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
            documentController.delegate = self;
        documentController.UTI = @"public.text";
        [documentController presentOpenInMenuFromRect:CGRectZero
                                       inView:self.view
                                     animated:YES];
        // Some more stuff
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-24
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    相关资源
    最近更新 更多