presentOpenInMenuFromBarButtonItem:不会导致菜单出现

Posted

技术标签:

【中文标题】presentOpenInMenuFromBarButtonItem:不会导致菜单出现【英文标题】:presentOpenInMenuFromBarButtonItem: doesn't cause menu to appear 【发布时间】:2014-05-13 06:32:47 【问题描述】:

我正在尝试通过UIDocumentInteractionControllerpresentOpenInMenuFromBarButtonItem 显示打开方式菜单。这不会在屏幕上显示UIDocumentInteractionController。奇怪的是,如果我将“OpenIn”替换为“Options”,那么它将按预期工作。

是什么导致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

【问题讨论】:

【参考方案1】:

问题是 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];
    

【讨论】:

以上是关于presentOpenInMenuFromBarButtonItem:不会导致菜单出现的主要内容,如果未能解决你的问题,请参考以下文章