大佬教程收集整理的这篇文章主要介绍了ios – 如何在Swift中以编程方式获取iPad / iPhone应用程序的大小,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
func getAppSize(){ let paths : NSArray = NSSearchPathForDirectoriesInDomains(.LibraryDirectory,.UserDomainMask,truE) var errorP = NSErrorPointer() let pathDictionary: NSDictionary = NSFileManager.defaultManager().attributesOfFileSystemForPath(paths.firstObject as String,error: errorp)! if(pathDictionary.count != 0){ var fileSystemSizeInBytes: Double = pathDictionary.objectForKey("NSFileSystemSize") as Double var fileSystemSizeInMegaBytes : Double = fileSystemSizeInBYTES/1000000 println("@R_564_10586@l Space: \(fileSystemSizeInMegaBytes) MB") }else{ } }
目前我只使用File-System Attirbute Key作为文件系统大小,我希望它是我当前应用程序的大小,但由于它返回249 GB,它显然只读取我的整个MACBook文件系统大小,而不是模拟器的文档路径.
我查找了其他属性键选项,发现:
let NSFileSystemSize: NSString! let NSFileSystemFreeSize: NSString! let NSFileSystemNodes: NSString! let NSFileSystemFreeNodes: NSString! let NSFileSystemnumber: NSString!
但是,我没有找到一个指定我的应用程序占用的实际空间大小的键.我想也许有办法获得所有节点,然后加上他们的大小,但我不太清楚如何做到这一点.
提前致谢
编辑:
因此捆绑包似乎给了我我的应用程序大小,但没有我保存到文档路径中的任何mp3文件.我猜我可能会将它们保存到错误的位置或者其他东西,这里是我将它们保存到应用程序的简化版本.
//Make http request Ect... before here... if(data.length > 1000){ let documentPath = NSSearchPathForDirectoriesInDomains(.LibraryDirectory,truE) let uuid = NSUUID().UUIDString let localUrl = uuid + ".mp3" let destPath = (documentPath[0] as String) + "/" + localUrl data.writeToFile(destPath,atomically: truE) var error: NSErrorPointer = nil data.writeToFile(destPath,options: NSDataWriTingOptions.DataWriTingAtomic,error: error) if(error != nil){ println("data write error: \(error.memory?.localizedDescription)") } }
要获取应用程序的总大小,请枚举包,文档,库和tmp子路径,例如:
func appSize() { // Go through the app's bundle let bundlePath = NSBundle.mainBundle().bundlePath let bundleArray:NSArray = NSFileManager.defaultManager().subpathsOfDirectoryAtPath(bundlePath,error: nil)! let bundleEnumerator = bundleArray.objectEnumerator() var fileSize: UInt64 = 0 while let filename:string = bundleEnumerator.nextObject() as? String { let fileDictionary:NSDictionary = NSFileManager.defaultManager().attributesOfItemAtPath(bundlePath.StringByAppendingPathComponent(fileName),error: nil)! fileSize += fileDictionary.fileSize(); } // Go through the app's document directory let documentDirectory:NSArray = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,truE) let documentDirectoryPath:NSString = documentDirectory[0] as NSString let documentDirectoryArray:NSArray = NSFileManager.defaultManager().subpathsOfDirectoryAtPath(documentDirectoryPath,error: nil)! let documentDirectoryEnumerator = documentDirectoryArray.objectEnumerator() while let file:string = documentDirectoryEnumerator.nextObject() as? String { let attributes:NSDictionary = NSFileManager.defaultManager().attributesOfItemAtPath(documentDirectoryPath.StringByAppendingPathComponent(filE),error: nil)! fileSize += attributes.fileSize(); } // Go through the app's library directory let libraryDirectory:NSArray = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory,NSSearchPathDomainMask.UserDomainMask,truE) let libraryDirectoryPath:NSString = libraryDirectory[0] as NSString let libraryDirectoryArray:NSArray = NSFileManager.defaultManager().subpathsOfDirectoryAtPath(libraryDirectoryPath,error: nil)! let libraryDirectoryEnumerator = libraryDirectoryArray.objectEnumerator() while let file:string = libraryDirectoryEnumerator.nextObject() as? String { let attributes:NSDictionary = NSFileManager.defaultManager().attributesOfItemAtPath(libraryDirectoryPath.StringByAppendingPathComponent(filE),error: nil)! fileSize += attributes.fileSize(); } // Go through the app's tmp directory let tmpDirectoryPath:NSString = NstemporaryDirectory() let tmpDirectoryArray:NSArray = NSFileManager.defaultManager().subpathsOfDirectoryAtPath(tmpDirectoryPath,error: nil)! let tmpDirectoryEnumerator = tmpDirectoryArray.objectEnumerator() while let file:string = tmpDirectoryEnumerator.nextObject() as? String { let attributes:NSDictionary = NSFileManager.defaultManager().attributesOfItemAtPath(tmpDirectoryPath.StringByAppendingPathComponent(filE),error: nil)! fileSize += attributes.fileSize(); } var fileSystemSizeInMegaBytes : Double = Double(fileSizE)/1000000 println("@R_564_10586@l App Space: \(fileSystemSizeInMegaBytes) MB") }
以上是大佬教程为你收集整理的ios – 如何在Swift中以编程方式获取iPad / iPhone应用程序的大小全部内容,希望文章能够帮你解决ios – 如何在Swift中以编程方式获取iPad / iPhone应用程序的大小所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。