问题
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
回答1:
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];
}
来源:https://stackoverflow.com/questions/23624218/presentopeninmenufrombarbuttonitem-doesnt-cause-menu-to-appear