我尝试通过 UIDocumentInteractionController
和 presentOpenInMenuFromBarButtonItem
显示“打开方式”菜单。这不会在屏幕上显示 UIDocumentInteractionController
。奇怪的是,如果我用“Options
”替换“OpenIn
”,那么它将按预期工作。
是什么导致 presentOpenInMenuFromBarButtonItem
不起作用?谢谢。
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
最佳答案
问题是,presentOpenIn... 仅在安装了可以打开您正在发送的文件的应用程序时才显示菜单。 iOS Simulator 没有任何打开 .txt 文件的应用程序,因此它似乎无法正常工作。如果您在物理设备上运行,它就可以正常工作。
我决定添加这个以获得更好的行为:
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];
}
关于ios - 当前OpenInMenuFromBarButtonItem : doesn't cause menu to appear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23624218/