Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

objective c - iPhone - Free space left on device reported incorrectly (+- 200 Mb difference)

I use this method to get the free space on the disk, extracted from a code found after some researches.

    float freeSpace = -1.0f;  
    NSError* error = nil;  
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSDictionary* dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];  

    if (dictionary) {  
        NSNumber* fileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];  
        freeSpace = [fileSystemSizeInBytes floatValue];  
    }

I wonder why when runing this, it gives me a free space of 3660062720.000000 bytes that would give 3,408699035644531 Gb (/1024/1024/1024)

But looking into my iPhone setting -> general info (and also into iTunes), I'm said that my iPhone has only 3.2 Gb left.

Where is the mistake ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It appears that sometimes the free space is reported incorrectly https://discussions.apple.com/thread/2566412?threadID=2566412

EDIT: I tried the following code and noticed that on my device, there was also a ~200MB discrepancy. Maybe that storage is reserved for the system somehow?

NSDictionary *fsAttr = [[NSFileManager defaultManager] fileSystemAttributesAtPath:NSHomeDirectory()];

unsigned long long freeSpace = [[fsAttr objectForKey:NSFileSystemFreeSize] unsignedLongLongValue];

NSLog(@"%llu", freeSpace);
NSLog(@"%f", freeSpace / 1073741824.0); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...