当前位置 : 主页 > 手机开发 > ios >

ios – 如何在Swift中以编程方式获取iPad / iPhone应用程序的大小

来源:互联网 收集:自由互联 发布时间:2021-06-11
所以我试图以编程方式获取我的应用程序的大小.该应用程序下载了大量的音频文件,我正在尝试检查当我删除文件时,应用程序大小会按预期减少.到目前为止,我知道如何做到这一点的唯
所以我试图以编程方式获取我的应用程序的大小.该应用程序下载了大量的音频文件,我正在尝试检查当我删除文件时,应用程序大小会按预期减少.到目前为止,我知道如何做到这一点的唯一方法是查看iPad的设置,但那里的数字似乎总是不正确.

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("Total 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, .UserDomainMask, 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)")
    }
}
According to the docs,“OS X和iOS中的文件系统处理数据文件,应用程序以及与操作系统本身相关的文件的持久存储.”同样是 according to the docs,“NSBundle对象表示文件系统中可以在程序中使用的代码和资源的位置”,因此您的应用程序的主包是其文件所在的位置.根据 this link,每个iOS应用程序都包含一个包,文档目录,库和tmp.

要获取应用程序的总大小,请枚举包,文档,库和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, .UserDomainMask, 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("Total App Space: \(fileSystemSizeInMegaBytes) MB")
}
网友评论