文件共享/在另一个应用程序中发送文件(在iOS中“打开”) [英] File sharing / send file in another app ("open in" in iOS)

查看:851
本文介绍了文件共享/在另一个应用程序中发送文件(在iOS中“打开”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

my app make a simple file called log.txt

此文件的URL(在xcode中查看)是file:// localhost / var / mobile / Applications / 应用程序号 /Documents/log.txt

the URL of this file (viewed in xcode) is file://localhost/var/mobile/Applications/NUMBER OF THE APPLICATION/Documents/log.txt

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

So I can see this file in the finder ...

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

I wanted to add the "open in" feature to my app to provide the user to share this file (via mail or imessage) or open this file in another compatible app.

以下是我的工作:

-(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的路径之外没有任何附加...

When I run the app, I click on this "share" button, but nothing appends except showing me the path of the URL in the log window ...

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

I missed something ...

谢谢

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

EDIT : finally, it works on my real iphone ... there was no text viewer in the simulator !!! --'

编辑2:它显示可用的应用程序(页面,碰撞...)但最终崩溃:((!请点击此处查看崩溃图片

EDIT 2 : it shows the apps that are available (pages, bump ...) but crashes finally :((( ! see here for the crash picture

推荐答案

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

Its a memory management issue. The main reason it crashes is because the object is not retained. Thats why if you declare it in the .h file and write an @property for retain when you do assign it the object gets retained.

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

So in your interface file (.h) you should have

@property (retain)UIDocumentInteractionController *documentController;

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

Then in your .m (implementation file) you can do

@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
}

这篇关于文件共享/在另一个应用程序中发送文件(在iOS中“打开”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆