presentOpenInMenuFromBarButtonItem: doesn't cause menu to appear

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-04 06:29:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!