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

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

问题描述

我的应用程序刚刚被 Apple 拒绝,因为它在文档目录中存储了临时文件或缓存文件.对.他们的拒绝消息指出:

My app has just been rejected by Apple because it was storing temporary or cache files in the documents directory. Right. Their rejection message states:

应用使用的临时文件只能存放在/tmp目录

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-3609Fzipttemp.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 空间中,应写入用户的文档"目录

属于和扩展您的应用程序的数据(例如,扩展用户可以下载...),但不在捆绑包中,应写入应用程序支持/"的子文件夹中目录,具有您的 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 页面确定应用特定文件的存储位置"部分的说明.

Check this official iOS developement Apple page in section "Determining Where to Store Your App-Specific Files" for explanations.

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

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

斯威夫特:

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天全站免登陆