presentOpenInMenuFromBarButtonItem: doesn't cause menu to appear
Asked Answered
T

1

2

I am attempting to display an Open In menu via UIDocumentInteractionController and presentOpenInMenuFromBarButtonItem. This does not bring up the UIDocumentInteractionController on screen. The weird thing is that if I replace "OpenIn" with "Options" then it will work as expected.

What is causing presentOpenInMenuFromBarButtonItem not to work? Thank you.

    NSString *fileName = [NSString stringWithFormat:@"%@text.txt", NSTemporaryDirectory()];
    [self.textToShare writeToFile:fileName
                       atomically:NO
                         encoding:NSUTF8StringEncoding
                            error:nil];

    NSURL *textFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"text.txt"]];

    self.openInController = [UIDocumentInteractionController interactionControllerWithURL:textFileURL];
    self.openInController.delegate = self;
    [self.openInController presentOpenInMenuFromBarButtonItem:self.buttonToPresentFrom animated:YES]; //replacing OpenIn with Options causes it to appear
Trifle answered 13/5, 2014 at 6:32 Comment(0)
T
3

The problem is that presentOpenIn... only displays a menu when there are apps installed that can open the file you're sending. iOS Simulator does not have any apps that open .txt files, so it didn't seem to be working. If you run on physical device, it works just fine.

I decided to add this for better behavior:

BOOL didPresentOpenIn = [self.openInController presentOpenInMenuFromBarButtonItem:self.buttonToPresentFrom animated:YES];
    if (!didPresentOpenIn) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Apps Available"
                                                        message:@"You do not have any apps installed that can open text files."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
Trifle answered 14/5, 2014 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.