【问题标题】:How to get storage and memory size of my iOS Application?如何获取我的 iOS 应用程序的存储和内存大小?
【发布时间】:2017-12-15 01:29:59
【问题描述】:

在我的情况下,我希望在我的应用程序存储空间达到一定水平时显示警报。我看到StackOverflow中提到的一些代码

  1. 从网络上清除临时缓存和图像
  2. 克服所有存储内存大小
  3. RAM 大小等,

我没有找到任何与我想要的东西相关的东西。如果我遗漏了什么,请指出。

简单明了:想要我的应用程序的存储空间大小,如 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。但是我得到了这个代码来获得空闲内存。其实是获取空闲内存还是获取应用内存。

【问题讨论】:

    标签: ios size local-storage storage


    【解决方案1】:

    要获取存储信息,请使用以下代码 (Swift 5)。

    //
    //  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())
    
                return attributes[FileAttributeKey.systemFreeSize] as! Int64
            }
            catch
            {
                return 0
            }
        }
    
        static func getTotalSpace() -> Int64
        {
            do {
                let attributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory())
                return attributes[FileAttributeKey.systemFreeSize] as! Int64
            } 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())
    

    【讨论】:

    • 谢谢,但我只需要我的应用程序的信息。不适用于整体移动信息。
    • 操作,我明白了 :) @user3589771
    • @roy 你有没有得到任何解决方案来单独获取你的应用程序的存储信息?
    • @Sharme,我没有
    • 每次调用 getFreeSpace() 时都会获取随机数据
    猜你喜欢
    • 2020-07-13
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 2015-11-12
    • 2012-03-25
    • 1970-01-01
    相关资源
    最近更新 更多