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

dams net picture dams net · Mar 12, 2013 · Viewed 7.7k times · Source

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

Answer

fzaziz picture fzaziz · Apr 17, 2013

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
}