【问题标题】:How to find size of my iPhone app programmatically?如何以编程方式查找我的 iPhone 应用程序的大小?
【发布时间】:2012-07-28 21:19:33
【问题描述】:

我正在努力寻找 iphone 应用程序的大小以及框架,但我没有找到如何做到这一点的方法。谁能提供我的代码?我得到了在运行时查找我的应用程序当前内存使用情况的代码。

link is here

【问题讨论】:

标签: iphone ios ipad


【解决方案1】:
NSString *folderPath = [[NSBundle mainBundle] bundlePath];
NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:folderPath error:nil];
NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
NSString *fileName;
unsigned long long int fileSize = 0;

while (fileName = [filesEnumerator nextObject]) {
    NSDictionary *fileDictionary = [[NSFileManager defaultManager] fileAttributesAtPath:[folderPath stringByAppendingPathComponent:fileName] traverseLink:YES];
    fileSize += [fileDictionary fileSize];
}

NSLog(@"App size : %lld",fileSize);

【讨论】:

    【解决方案2】:

    如果有人还在寻找答案,请注意 fileAttributesAtPath 方法已被贬低,因此请使用 attributesOfItemAtPath 方法

    例子:

    //get full pathname of bundle directory
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    
    //get paths of all of the contained subdirectories
    NSArray *bundleArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:bundlePath error:nil];
    
    //to access each object in array
    NSEnumerator *filesEnumerator = [bundleArray objectEnumerator];
    
    NSString *fileName;
    
    unsigned long long int fileSize = 0;
    NSError *error = nil;
    
    //return next object from enumerator
    while (fileName = [filesEnumerator nextObject])
    {
        NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:[bundlePath stringByAppendingPathComponent:fileName] error:&error];
    
        fileSize += [fileDictionary fileSize];
    }
    //converts a byte count value into a textual representation that is formatted with the appropriate byte modifier (KB, MB, GB and so on)
    NSString *folderSizeStr = [NSByteCountFormatter stringFromByteCount:fileSize countStyle:NSByteCountFormatterCountStyleMemory];
    NSLog(@"App size (bundle size): %@ \n\n\n",folderSizeStr);
    

    【讨论】:

      【解决方案3】:

      快速回答 - SWIFT 3.x

      let bundlePath = Bundle.main.bundlePath
      let bundleArray  = FileManager.default.subpaths(atPath: bundlePath)
      var fileSize : UInt64 = 0
      for file in bundleArray! {
          do {
              let attr = try FileManager.default.attributesOfItem(atPath: bundlePath + "/" + file )
              let xfileSize = attr[FileAttributeKey.size] as? UInt64 ?? 0
              fileSize =  fileSize + xfileSize
          } catch {
          }
      }
      let folderSize = ByteCountFormatter.string(fromByteCount: Int64(fileSize), countStyle: .memory)
      print("App size (bundle size): \(folderSize)")//xx MB
      

      这将为您提供捆绑包中的应用程序大小 -

      /Users/MyLaptop/Library/Developer/CoreSimulator/Devices/DB7032C9-7CE2-42A9-83D1-59E2D2C5C361/data/Containers/Bundle/Application/CC808040-2F18-4B35-A912-591BD0C7DCD9/MyApp.app

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-25
        • 1970-01-01
        • 1970-01-01
        • 2011-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多