OS X App沙箱和任意文件访问-更新为基于文档的吗? [英] OS X App Sandboxing and arbitrary files access - Update to Document-based?

查看:129
本文介绍了OS X App沙箱和任意文件访问-更新为基于文档的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的OS X应用程序(当前未沙盒化)访问用户设置的目录中包含的文件(用户使用NSOpenPanel选择路径,并在整个执行过程中保留对此路径的引用).文件列表是通过NSDirectoryEnumerator生成的,然后我分别使用AVAssettaglib(在具有桥接头的C ++中)读取和写入这些文件.

My OS X app (currently not sandboxed) accesses files contained inside a directory set by the user (one chooses the path with a NSOpenPanel and a reference to this path is kept throughout execution). The list of files is generated via NSDirectoryEnumerator and I then read from and write to those files using AVAsset and taglib (in C++ with a bridging header) respectively.

如预期的那样,在Xcode中启用沙箱功能使该应用程序无用,NSDirectoryEnumerator给出的文件列表为空,即使不是,我也无法读取和写入文件.要使我的应用符合沙盒要求,我需要采取哪些步骤?

As expected, enabling Sandboxing in Xcode rendered the app useless, the list of files given by NSDirectoryEnumerator is empty and even if it weren't, I would not be able to read from and write to the files. What are the steps I need to take to make my app sandbox-compliant?

我的应用是否需要基于文档?因为我实际上没有适当的文档(例如:我没有每个文件的窗口,它似乎不符合标准的基于文档的应用程序模型),所以我的应用程序真的可以是基于文档的"吗?我的应用程序基本上只是一个表视图,其中文件引用为行. 另一个要点:如果我的应用程序是基于文档的,我仍然可以使用taglib来写入文件吗?我需要将taglib的文件路径作为字符串指针传递给它,以使其正常工作.

Does my app need to be document based? Can my app really be "document-based" since I don't really have proper documents (as in: I don't have a window per file, it doesn't seem to comply to the standard document-based app model)? My app is basically just a table view with files references as rows. Another important point: can I still use taglib to write to my files if my app is document-based ? I need to pass taglib the path to my file as a string pointer in order for it to work.

非常感谢,此主题目前令人困惑.

Thanks a lot, this topic is quite confusing at the moment.

推荐答案

您不必将应用转换为基于文档的方式即可访问用户选择的文件和安全范围内的书签.

You don't have to convert your app to be document-based to gain access to user selected files and security scoped bookmarks.

我可以想到两个原因导致您当前的代码无法在沙盒环境中运行:

I can think of 2 reasons why your current code does not work in a sandboxed environment:

  • 您没有设置用户选择文件访问"功能(Xcode>目标>功能>应用沙箱>文件访问)
  • 您正在使用目录枚举器的基于path/NSString的API,而不是基于URL NSURL的API.

已启用沙箱并且设置了用户选择的文件"功能的香草Xcode项目应枚举通过NSOpenPanel获得的所有路径:

A vanilla Xcode project with Sandboxing enabled and the User selected files capabilities set, should enumerate any path obtained via NSOpenPanel:

NSOpenPanel* panel =[NSOpenPanel openPanel];
panel.canChooseDirectories = YES;
[panel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result) {
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSURL *directoryURL = panel.URL;
    NSDirectoryEnumerator *enumerator = [fileManager
                                         enumeratorAtURL:directoryURL
                                         includingPropertiesForKeys:nil
                                         options:0
                                         errorHandler:nil];
    for (NSURL *url in enumerator) { 
        NSLog(@"url:%@", url);
    }
}];

如果要存储在应用程序启动/退出周期中从沙箱访问特定文件夹的功能,则需要存储安全范围的书签. 这篇文章包含通过应用程序范围的书签持久保存用户选择的文件/目录访问的信息: 创建安全范围内的书签时遇到问题

If you want to store the ability to access specific folders from the sandbox across app launch/quit cycles, you will need to store a security scoped bookmark. This post contains information persisting user selected file/directory access via app scoped bookmark: Trouble creating Security-Scoped Bookmark

这篇关于OS X App沙箱和任意文件访问-更新为基于文档的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆