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