10

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

dams net
  • 301
  • 3
  • 13
  • Are you sure that there is an app installed on your device that can open the file type? – Daniel Martín Mar 12 '13 at 15:51
  • see edit and edit 2 in my post – dams net Mar 12 '13 at 16:10
  • Open the Breakpoint Navigator, click the + sign in the lower right corner, Add Exception Breakpoint, Done. Run the application again and now the debugger will stop when the exception is thrown. – Daniel Martín Mar 12 '13 at 16:17
  • 1
    2013-03-12 17:27:00.545 newapp[1930:907] -[__NSCFType _openDocumentWithApplication:]: unrecognized selector sent to instance 0x1dd7e220 does this mean that my txt file cannot be open with pages ??? – dams net Mar 12 '13 at 16:28

2 Answers2

42

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
}
fzaziz
  • 697
  • 4
  • 8
  • Any idea about my question. http://stackoverflow.com/questions/30613645/add-edit-in-exel-or-edit-photo-extension – Durgaprasad Jun 04 '15 at 09:40
0

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

dams net
  • 301
  • 3
  • 13