文件共享/在另一个应用程序中发送文件(在iOS中“打开”)
我的应用程序创建了一个名为log.txt的简单文件
此文件的URL(在xcode中查看)是file:// localhost / var / mobile / Applications / NUMBER OF THE APPLICATION /Documents/log.txt
所以我可以在finder中看到这个文件……
我想在我的应用程序中添加“打开”function,以便用户共享此文件(通过邮件或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:它显示可用的应用程序(页面,碰撞…)但最终崩溃:((( 见这里的崩溃图片
它是一个内存管理问题。 崩溃的主要原因是因为没有保留对象。 这就是为什么如果你在.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 }
以下是它对我有用的方式:
我只是将声明“UIDocumentInteractionController * documentController”放在.h文件中,它就可以了!
我真的不知道为什么,但….