读取手机内存

2018-06-05  本文已影响84人  小小鱼类
读取手机内存
#include <sys/param.h>
#include <sys/mount.h>

- (void) memoryCount{
     // 总大小
    float totalsize = 0.0;
    // 剩余大小
    float freesize = 0.0;
    //  是否登录
    NSError *error = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
    if (dictionary)
    {
        NSNumber *_free = [dictionary objectForKey:NSFileSystemFreeSize];
        freesize = [_free unsignedLongLongValue]*1.0/(1024);
        
        NSNumber *_total = [dictionary objectForKey:NSFileSystemSize];
        totalsize = [_total unsignedLongLongValue]*1.0/(1024);
    } else
    {
        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
    }
    NSLog(@"totalsize = %.2f, freesize = %f",totalsize/1024/1024/1024, freesize/1024);

}
+ (NSString *)freeDiskSpaceInBytes{
    struct statfs buf;
    unsigned long long freeSpace = -1;
    if (statfs("/var", &buf) >= 0) {
        freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail);
    }
     NSString *str = [NSString stringWithFormat:@"手机剩余存储空间为:%0.2lld MB",freeSpace/1024/1024];
    return str;
}

文章参考:
https://www.jianshu.com/p/a9ebf2aff7ac

小小总结,不成敬意

上一篇 下一篇

猜你喜欢

热点阅读