ios - 当前OpenInMenuFromBarButtonItem : doesn't cause menu to appear

标签 ios objective-c uiactivityviewcontroller uidocumentinteraction uiactivity

我尝试通过 UIDocumentInteractionControllerpresentOpenInMenuFromBarButtonItem 显示“打开方式”菜单。这不会在屏幕上显示 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/

相关文章:

ios - 检测 UITextField 何时更改而不使用键盘快速

iPhone - 找不到时调试崩溃

objective-c - Objective-C 中一行中的多个逗号分隔调用

objective-c - 如何将字符插入 NSString

ios - 使用 UIActivityViewController 共享多个图像

ios - 使用 UIActivityViewController 发送的 UIImage 的控制文件名

ios - 更改 UIActivityController 的标题文本颜色

objective-c - 我必须在这里释放 OpenAL 上下文吗?

ios - 带有 Swift 4/Xcode 10 : Argument type 'SDKLoggingBehavior?' does not conform to expected type 'Sequence' 的 Facebook SDK

c++ - Mac(或 C++)连接到二进制 WCF