File sharing / send file in another app (“open in”

2020-06-12 06:12发布

my app make a simple file called log.txt

the URL of this file (viewed in xcode) is file://localhost/var/mobile/Applications/NUMBER OF THE APPLICATION/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.

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

I missed something ...

Thanks

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

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

2条回答
欢心
2楼-- · 2020-06-12 06:52

Here is how it works for me :

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

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

查看更多
Deceive 欺骗
3楼-- · 2020-06-12 06:55

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.

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

@property (retain)UIDocumentInteractionController *documentController;

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
}
查看更多
登录 后发表回答