How to acquire a file path using objective c

放肆的年华 提交于 2019-12-05 20:28:13

The code looks fine. Try calling your function from main thread; modal dialogs need this and usually don't enforce it themselves. In any case the Powebox (the thing that runs NSOpenPanel in sandbox) is a bit of a beast. Also you might get different results with debug build launched directly from Xcode (not so good) and release build launched from Finder.

To test NSOpenPanel: Try running your App under a guest account to eliminate all stuff that's cluttering the sandbox .

ingconti

My code: (actually in production:)

-(void)OpenIt {
    NSOpenPanel* panel = [NSOpenPanel openPanel];

    // This method displays the panel and returns immediately.
    // The completion handler is called when the user selects an
    // item or cancels the panel.

    [panel setTitle: "title...."];
    NSString *title = NSLocalizedString(@"CHOOSE_FILE", @"");
    [panel setMessage: title];

    [panel beginWithCompletionHandler:^(NSInteger result){
        if (result == NSFileHandlingPanelOKButton) {
            NSURL*  theDoc = [[panel URLs] objectAtIndex:0];
            NSLog(@"%@", theDoc);
            // Open  the document using url
            [self openUsingURL: theDoc];
        }
    }];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!