2016 年更新答案:
用户明确接受的个人数据,并可能备份在他/她的 iCloud 空间中,应写入用户的“文档”目录
属于和扩展您的应用程序的数据(例如,扩展用户可以下载,...),但不在捆绑包中,应写入“Application Support/”目录的子文件夹,标题为您的 appID。它也可以是“缓存”目录。
生命周期较短的数据可以存储在应用程序的 tmp 目录中。在这种情况下,使用 NSTemporaryDirectory() 可以获得“tmp”目录。检查此链接以获得更多帮助。
查看“确定在哪里存储您的应用程序特定文件”部分中的官方 iOS 开发 Apple 页面以获取解释。
以下是 Swift 中的 3 个函数,旨在将 NSURLs 返回到这些目录并使您的喜欢更简单。
斯威夫特:
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)
}