Pregunta

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
¿Fue útil?

Solución

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];
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top