presentOpenInMenuFromBarButtonItem:不会导致菜单出现

时间:2014-05-13 06:32:47

标签: ios objective-c uiactivityviewcontroller uidocumentinteraction uiactivity

我正在尝试通过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 个答案:

答案 0 :(得分:3)

问题是presentOpenIn ...仅在安装了可以打开您正在发送的文件的应用程序时显示菜单。 iOS模拟器没有任何打开.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];
    }