2017-07-11 98 views
0

在我的情况下,当我的应用程序存储空间达到一定水平时,想显示警报。我看到一些在StackOverflow上的代码从Web 如何获取iOS应用程序的存储空间和内存大小?

  • 克服所有的存储内存大小
  • RAM大小等。何况

    1. 明确临时高速缓存存储器和图像,

    我没找不到任何我想要的东西。如果我错过任何指出的话。

    简单明了:想要存储大小为我的应用,Android这样的显示应用程序的存储占用的大小(设置)

    更新1:

    进入设置>常规>存储&的iCloud用法>管理存储 我的应用程序显示20 MB也提及Documents 7 Data

    -(natural_t) get_free_memory { 
    mach_port_t host_port; 
    mach_msg_type_number_t host_size; 
    vm_size_t pagesize; 
    host_port = mach_host_self(); 
    host_size = sizeof(vm_statistics_data_t)/sizeof(integer_t); 
    host_page_size(host_port, &pagesize); 
    vm_statistics_data_t vm_stat; 
    
    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) { 
        NSLog(@"Failed to fetch vm statistics"); 
        return 0; 
    } 
    
    /* Stats in bytes */ 
    natural_t mem_free = vm_stat.free_count * pagesize; 
    NSLog(@"mem_free %u", mem_free); 
    return mem_free; 
    } 
    

    我使用这个代码检查这个也给了20 MB左右。但是我得到这个代码以获得可用内存。实际上是获取空闲内存或获取应用程序内存。

  • 回答

    0

    要获取存储信息,请使用下面的代码(Swift 3)。

    // 
    // Storage.swift 
    // BCScanner2 
    // 
    // Created by Admin on 2017-07-11. 
    // Copyright © 2017 com.odyssey. All rights reserved. 
    // 
    
    import Foundation 
    
    class Storage 
    { 
    
        static func getFreeSpace() -> Int64 
        { 
         do 
         { 
          let attributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String) 
    
          return ((attributes[FileAttributeKey.systemFreeSize] as? NSNumber)?.int64Value)! 
         } 
         catch 
         { 
          return 0 
         } 
        } 
    
        static func getTotalSpace() -> Int64 
        { 
         do { 
          let attributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String) 
          return ((attributes[FileAttributeKey.systemSize] as? NSNumber)?.int64Value)! 
         } catch { 
          return 0 
         } 
        } 
    
        static func getUsedSpace() -> Int64 
        { 
         return getTotalSpace() - getFreeSpace() 
        } 
    
    
    
    
    } 
    
    
    
    
    extension Int64 
    { 
        func toMB() -> String { 
         let formatter = ByteCountFormatter() 
         formatter.allowedUnits = ByteCountFormatter.Units.useMB 
         formatter.countStyle = ByteCountFormatter.CountStyle.decimal 
         formatter.includesUnit = false 
         return formatter.string(fromByteCount: self) as String 
        } 
    } 
    

    并呼吁喜欢。 。

    print(Storage.getTotalSpace().toMB()) 
    print(Storage.getFreeSpace().toMB()) 
    print(Storage.getUsedSpace().toMB()) 
    
    +0

    谢谢,但我只需要我的应用程序的信息。不适用于整体移动信息。 – user3589771

    +0

    ops,我看到:) @ user3589771 – Roy

    相关问题