5

Is it possible to get the total size of NSUserDefaults? because what I know it saves everything in a .plist file. It shouldn't be too hard to find that file and then see how big it is.

Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • this `.plist` file present inside `cache` folder. – Yogesh Suthar Jul 31 '14 at 07:46
  • The plist file is present under `AppDirectory/Library/Preferences/.plist`. Try to read the [attributes of this file](http://stackoverflow.com/a/5743957/1407017). – Amar Jul 31 '14 at 08:01
  • @Amar is that folder inside iphone/iphone simulator? – Arbitur Jul 31 '14 at 08:08
  • Its your apps directory which gets created when you install it on device/simulator. On simulator the path of this is `/Users//Library/Application Support/iPhone Simulator//Applications/3FC9264F-371B-4DFC-8714-2726E45B0D6F/Library/Preferences` – Amar Jul 31 '14 at 08:12
  • @Amar I followed your link and made this in Swift: `let fileAttr:NSDictionary = NSFileManager.defaultManager().attributesOfItemAtPath("AppDirectory/Library/Preferences/\(NSBundle.mainBundle().bundleIdentifier).plist", error: nil); let size = fileAttr.objectForKey(NSFileSize).longLongValue`But it crashes and doesnt find it. Both in simulator and device. – Arbitur Jul 31 '14 at 08:26
  • 1
    You should not hard code `AppDirectory` path ... need to do it programmatically and append the remaining path including the plist file name and extension. http://stackoverflow.com/a/3763050/1407017 – Amar Jul 31 '14 at 08:32

2 Answers2

5

Try this code,

NSString* libraryDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* filepath = [libraryDir stringByAppendingPathComponent:[NSString stringWithFormat:@"/Preferences/%@.plist",[[NSBundle mainBundle] bundleIdentifier]]];
long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:nil][NSFileSize] longLongValue];

Code referenced from link1, link2 which I mentioned in my comments.

You may have to convert this to use in Swift.

Hope that helps!

Community
  • 1
  • 1
Amar
  • 13,202
  • 7
  • 53
  • 71
4

Updated to Swift-4

static func getSizeOfUserDefaults() -> Int? {
    guard let libraryDir = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first else {
        return nil
    }

    guard let bundleIdentifier = Bundle.main.bundleIdentifier else {
        return nil
    }

    let filepath = "\(libraryDir)/Preferences/\(bundleIdentifier).plist"
    let filesize = try? FileManager.default.attributesOfItem(atPath: filepath)
    let retVal = filesize?[FileAttributeKey.size]
    return retVal as? Int
}
  • 1
    Is this size in bytes? bits? kb? – Ahmadreza Jun 22 '19 at 04:34
  • 1
    Hey @Alfi the documentation says the size is in bytes. https://developer.apple.com/documentation/foundation/fileattributekey/1416548-size If you option click on the .size property in Xcode, you can get this info. – CristianMoisei Jul 03 '19 at 18:30