1

Is there any way to programmatically (in Objective-C) find out the marketed total physical memory of an iOS devices (such as iPad 2 16G, 32G, 64G)? It looks like UIDevice related APIs (such as discussed here and here) do not give such information.

Thanks!

Community
  • 1
  • 1
Zebra Propulsion Lab
  • 1,630
  • 1
  • 15
  • 24

2 Answers2

3

please see here. You can get the device total space as well as the free space available. It gives you the clear idea.

Community
  • 1
  • 1
Girish
  • 5,094
  • 4
  • 33
  • 53
2

A little late to the party but if anyone still gets here, there you go:

This one gives you the memory size as marketed on the iOS device (16GB, 32GB, 64GB, 128GB...)

Calculated using the method suggested by others and then rounded off to the nearest bigger power of 2.

Swift 3.0 version

extension UIDevice {

var marketedMemorySize: Int {

    do {
        let systemAttributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String)
        let totalDiskSpaceInBytes = (systemAttributes[FileAttributeKey.systemSize] as? NSNumber)?.int64Value

        func formatter(_ bytes: Int64) -> String {
            let formatter = ByteCountFormatter()
            formatter.allowedUnits = ByteCountFormatter.Units.useGB
            formatter.countStyle = ByteCountFormatter.CountStyle.decimal
            formatter.includesUnit = false
            return formatter.string(fromByteCount: bytes) as String
        }

        if let bytes = totalDiskSpaceInBytes {
            let memory = formatter(bytes)

            if let floatMemory = Float(memory) {

                if floatMemory > 128 {
                    return 256
                } else if floatMemory > 64 {
                    return 128
                } else if floatMemory > 32 {
                    return 64
                } else if floatMemory > 16 {
                    return 32
                } else if floatMemory > 8 {
                    return 16
                } else if floatMemory > 4 {
                    return 8
                }

            }
        }
        return 0
    }
    catch {
        return 0
    }

}


}

Use it like this:

UIDevice.current.marketedMemorySize
Vinay Kharb
  • 131
  • 1
  • 4