在沙箱中重新启动后访问同一文件 [英] Access to the same file after restart in the sandbox

查看:90
本文介绍了在沙箱中重新启动后访问同一文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在

My app reads iTunes' XML Library file under

/Users/username/Music/iTunes/iTunes Music Library.xml

但是,如果iTunes库存储在其他硬盘上,则此文件也将存储在其他硬盘上. 除非用户授权,否则沙盒不允许我访问此文件.

However, if the iTunes Library is stored on a different hard disk, so will this file. The sandbox doesn't allow me to access this file, unless the user grants it.

这是通过NSOpenPanel完成的.

但是,现在我每次打开应用程序时都必须询问用户访问此文件的权限,这很烦人.

However, now I'd have to ask the user for permission to access this file each time he opens up the app, which is pretty annoying.

是否可以永久授予对特定文件的访问权限?

Is there a way to permanently grant access to a specific file?

例如,如果您在另一个应用程序中打开文件,则如果退出该文件,则该应用程序将再次打开相同的文件.像这样的东西会有用.

For example, if you open a file in another app, if you quit it the app will open the same file again. Something like this would be useful.

推荐答案

您需要使用安全范围内的书签.您需要做的概述是:

What you need is to use security scoped bookmarks. The outline of what you need to do is:

  1. 检查您是否有保存的书签(请参见下文),如果已激活,请激活它.完成.
  2. 让用户使用NSOpenPanel
  3. 选择文件/文件夹
  4. 获取返回的URL并将其作为安全范围的书签保存到磁盘-您将其保存在容器中的某个位置,您的NSUserDefaults是一种选择.
  1. Check if you have a saved bookmark (see below), if so activate it. Done.
  2. Have the user select a file/folder using NSOpenPanel
  3. Take the URL that is returned and save it to disk as a security scoped bookmark - you save it somewhere in your container, your NSUserDefaults is one choice.

就是这样.您需要阅读

That's it. You need to read Apple's documentation to understand the details.

这篇关于在沙箱中重新启动后访问同一文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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