Mac 应用程序的开发比较少,所以搜索的资料比较少,这里记录一下我自己的经验,以作备份。
如图可以在sandbox中勾选自己需要的权限和文件夹访问权限,里面有一个User Selected File,这个比较实用
为此我们可在需要选择文件夹的地方添加如下代码
__weak typeof(self)weakSelf = self;
NSOpenPanel* panel = [NSOpenPanel openPanel];
panel.canCreateDirectories = YES;
panel.canChooseDirectories = YES;
panel.canChooseFiles = YES;
[panel setAllowsMultipleSelection:NO];
[panel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result)
{
if (result == NSModalResponseOK)
{
//NSURL *pathUrl = [panel URL];
NSString *pathString = [panel.URLs.firstObject path];
weakSelf.urlTextField.stringValue = pathString;
}
}];
// [panel beginWithCompletionHandler:^(NSInteger result){}];
需要提的是此方法需要在window创建成功以后调用,否则会报 unknow window class;