文件共享/在另一个应用程序中发送文件(在iOS中"打开")

dam*_*net 10 iphone xcode share file ios

我的应用程序创建一个名为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];
}
Run Code Online (Sandbox Code Playgroud)

然后调用这个函数:

-(IBAction)share:(id)sender {
    [self openDocumentIn];
}
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,我点击这个"共享"按钮,但除了在日志窗口中显示URL的路径之外没有任何附加内容...

我错过了什么......

谢谢

编辑:最后,它适用于我真正的iPhone ...模拟器中没有文本查看器! - "

编辑2:它显示可用的应用程序(页面,碰撞......)但最终崩溃:(((见这里的崩溃图片

fza*_*ziz 41

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

所以在你的界面文件(.h)中你应该有

@property (retain)UIDocumentInteractionController *documentController;
Run Code Online (Sandbox Code Playgroud)

然后在你的.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
}
Run Code Online (Sandbox Code Playgroud)


dam*_*net 0

它对我来说是这样的:

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

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