iOS 临时文件夹位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11897825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS temporary folder location
提问by philippe
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:
我的应用程序刚刚被 Apple 拒绝,因为它在文档目录中存储了临时文件或缓存文件。对。他们的拒绝消息指出:
Temporary files used by your app should only be stored in the
/tmp
directory
您的应用程序使用的临时文件应仅存储在
/tmp
目录中
I suppose it is that besides the Documentsand Libraryin the Application's folder.
我想除了应用程序文件夹中的文档和库之外。
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
.
我现在试图在 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
。
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 ?
那么:NSTemporaryDirectory()
使用 iPhone 模拟器是否有不同的行为,是否可以在调试时跟踪应用程序的临时目录?
采纳答案by philippe
UPDATED 2016 ANSWER :
2016 年更新的答案:
Data which is explicitly accepted by the user as personal, and potentially backuped in his/her iCloud space, should be written in user's "Documents" directory
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.
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 linkfor additional help.
被用户明确接受为个人的数据,并可能备份在他/她的 iCloud 空间中,应写入用户的“文档”目录中
属于和扩展您的应用程序(例如,扩展用户可以下载...)但不在包中的数据应写入“Application Support/”目录的子文件夹中,并具有您的 appID 的标题。它也可以是“缓存”目录。
生命周期较短的数据可以存储在应用程序的 tmp 目录中。在这种情况下,可以使用 NSTemporaryDirectory() 来获取“tmp”目录。检查此链接以获取更多帮助。
Check this official iOS developement Apple pagein section "Determining Where to Store Your App-Specific Files" for explanations.
在“确定存储应用程序特定文件的位置”部分中查看此官方 iOS 开发 Apple 页面以获取解释。
Below are 3 functions in Swift designed to return NSURLs to these directories and make your like simpler.
下面是 Swift 中的 3 个函数,旨在将 NSURL 返回到这些目录并使您的喜欢更简单。
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)
}
回答by Leo Dabus
iOS 9 or later ? Swift 3 or later
iOS 9 或更高版本?Swift 3 或更高版本
let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
iOS 10.0+Beta, macOS 10.12+, tvOS 10.0+Beta & watchOS 3.0+ ? Xcode 8 ? Swift 3 or later
iOS 10.0+Beta、macOS 10.12+、tvOS 10.0+Beta 和 watchOS 3.0+ ? Xcode 8 ? Swift 3 或更高版本
let tmpDirURL = FileManager.default.temporaryDirectory
回答by philippe
I have tested this on a real device, and it returned : "/private/var/mobile/Applications/C82383-EBD6-4F72-BC16-A865478D27/tmp/tempzip.zip"
我已经在真实设备上对此进行了测试,它返回:“/private/var/mobile/Applications/C82383-EBD6-4F72-BC16-A865478D27/tmp/tempzip.zip”
So overall, using NSTemporaryDirectory()
is the correct way of finding the path to the temporary directory, and that if you want to debug and view what is done within, you need to find it manually in the Finder if you are using the iPhone Simulator.
所以总的来说, usingNSTemporaryDirectory()
是找到临时目录路径的正确方法,如果您想调试和查看其中的内容,如果您使用的是 iPhone 模拟器,则需要在 Finder 中手动找到它。
Check newer answer below (this one is deprecated)
检查下面较新的答案(此答案已弃用)
回答by Hari Karam Singh
According to the docs, you should avoid NSTemporaryDirectory()
in favour of this approach
根据该文件,你应该避免NSTemporaryDirectory()
赞成这种办法的
- (NSURL)URLForTemporaryFolder
{
// Get a parent folder, trying user folder (fails iOS) and falling back to AppSupport and Docs
NSURL *parentFolderURL = [NSURL URLForDirectory:NSUserDirectory domainMask:NSUserDomainMask];
if (!parentFolderURL) parentFolderURL = [NSURL URLForDirectory:NSApplicationSupportDirectory domainMask:NSUserDomainMask];
if (!parentFolderURL) parentFolderURL = [NSURL URLForDirectory:NSDocumentDirectory domainMask:NSUserDomainMask];
// Get the temp folder URL using approach outlined in the docs
NSURL *tmpURL = [[NSFileManager defaultManager]
URLForDirectory:NSItemReplacementDirectory
inDomain:NSUserDomainMask
appropriateForURL:parentFolderURL
create:YES
error:NULL];
return tmpURL;
}
Be aware that this creates a newunique temp folder each time you call it and it's up to you to clean it up.
请注意,每次调用它时都会创建一个新的唯一临时文件夹,由您来清理它。