xcode 在另一个应用程序中共享/发送文件(iOS 中的“打开方式”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15365592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:57:24  来源:igfitidea点击:

File sharing / send file in another app ("open in" in iOS)

iphoneiosxcodefileshare

提问by dams net

my app make a simple file called log.txt

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

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

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

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

所以我可以在查找器中看到这个文件......

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.

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

Here is what I do :

这是我所做的:

-(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];
}

Then the call to this function :

然后调用这个函数:

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

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 ...

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

I missed something ...

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

Thanks

谢谢

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

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

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

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

回答by fzaziz

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 文件中声明它并在你分配它时为保留写一个 @property 对象被保留。

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

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

@property (retain)UIDocumentInteractionController *documentController;

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

然后在您的 .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
}

回答by dams net

Here is how it works for me :

这是它对我的工作方式:

I just put the declaration "UIDocumentInteractionController *documentController" in the .h file and it works !

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

I really don't know why, but ....

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