iOS临时文件夹位置 [英] iOS temporary folder location

查看:793
本文介绍了iOS临时文件夹位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这个问题转向Q& A,因为我找到答案,而
准备它。没关系,我觉得值得精确一点:

This question turned to a Q&A, because I found the answer while preparing it. Nevermind, I think it is worth precising this:

我的应用程序刚被Apple拒绝,因为它存储了临时文件或缓存文件文件目录。对。他们的拒绝消息说您的应用程序使用的临时文件应该只存储在 / tmp目录中。我想除了应用程序文件夹中的文档之外。

My app has just been rejected by Apple because it was storing temporary or cache files in the documents directory. Right. Their rejection message says that "Temporary files used by your app should only be stored in the /tmp directory". I suppose it is that besides the Documents and Library in the Application's folder.

我现在正在尝试调试这个问题在iPhone模拟器中,当我使用 NSTemporaryDirectory()时,我在XCode调试器中得到的值是 / var / folders / yj / gnz1c7156c7d6d4fj429yms40000gn / T / tempzip.zip ,而不是 / Users / me / Library / Application Support / iPhone Simulator / 5.1 / Applications / 8F71AB72-598C-427A-A116-36833D3209F7 / tmp / tempzip.zip

I am now trying to debug this issue in the iPhone Simulator, and when I use NSTemporaryDirectory(), the value I get in the XCode debugger is /var/folders/yj/gnz1c7156c7d6d4fj429yms40000gn/T/tempzip.zip, and not /Users/me/Library/Application Support/iPhone Simulator/5.1/Applications/8F71AB72-598C-427A-A116-36833D3209F7/tmp/tempzip.zip.

所以:是 NSTemporaryDirectory()有不同的使用iPhone模拟器的行为,是否可以在调试时跟踪应用程序的临时目录?

So : is NSTemporaryDirectory() having a different behaviour using the iPhone Simulator, and, is it possible to track the application's temporary directory at debug time ?

推荐答案

更新2016答案:


  • 用户明确接受为个人的数据,并可能在他/她的后备iCloud空间,应写在用户的文档中目录

属于并扩展您的应用程序的数据(扩展用户可以下载,例如......),但不在bundle,应该写在Application Support /目录的子文件夹中,具有appID的标题。它也可以是缓存目录

Data that belongs and extends your application (an extension user can download for instance,...), but which is NOT in the bundle, should be written in a subfolder of "Application Support/" directory, having the title of your appID. It can also be the "Cache" directory.

短生命时间的数据可以存储在你的tmp目录中应用。在这种情况下,使用NSTemporaryDirectory()可以获得tmp目录查看此链接以获取更多帮助。

Data with short-life time can be stored in the tmp directory of your application. In this case, use of NSTemporaryDirectory() is possible to get the "tmp" directory. Check this link for additional help.

选中官方iOS开发Apple页面确定应用程序专用文件的存储位置 用于解释。

以下是Swift中的3个函数,旨在将NSURL返回到这些目录并使您更简单。

Below are 3 functions in Swift designed to return NSURLs to these directories and make your like simpler.

Swift

func GetDocumentsDirectory()->NSURL{
    //returns User's "Documents" directory
    //something like this on a real device : file:///private/var/mobile/Containers/Data/Application/APPID/Documents/
    //something like this on the simulator : file:///Users/MACUSERID/Library/Developer/CoreSimulator/Devices/SIMDEVICEID/data/Containers/Data/Application/APPUUID/Documents/
    let filemgr = NSFileManager.defaultManager()
    let docsDirURL = try! filemgr.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
    return docsDirURL
}

func GetApplicationSupportDirectory()->NSURL{
    //returns Application's support directory
    //something like this on a real device : file:///private/var/mobile/Containers/Data/Application/APPID/Library/Application%20Support/YOURAPPBUNDLEID/
    //something like this on the simulator : file:///Users/MACUSERID/Library/Developer/CoreSimulator/Devices/SIMDEVICEID/data/Containers/Data/Application/APPUUID/Library/Application%20Support/YOURAPPBUNDLEID/
    let AllDirectories : [NSURL]
    var ApplicationSupportDirectory : NSURL=NSURL.init()
    var ApplicationDirectory : NSURL=NSURL.init()
    AllDirectories=NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)
    if AllDirectories.count>=1{
        ApplicationSupportDirectory=AllDirectories[0]
    }
    if !ApplicationSupportDirectory.isEqual(nil) {
        ApplicationDirectory=ApplicationSupportDirectory.URLByAppendingPathComponent(NSBundle.mainBundle().bundleIdentifier!)
    }
    return ApplicationDirectory
}

func GetTemporaryDirectory()->NSURL{
    //returns Application's temporary directory
    //something like this on a real device : file:///private/var/mobile/Containers/Data/Application/APPID/tmp/
    //something like this on the simulator : file:///Users/MACUSERID/Library/Developer/CoreSimulator/Devices/SIMDEVICEID/data/Containers/Data/Application/APPUUID/tmp/
    return NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
}

这篇关于iOS临时文件夹位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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