获取手机存储空间

#import sys/param.h

#import sys/mount.h


- (NSString*)freeDiskSpaceInBytes{

    structstatfsbuf;

    unsignedlonglongfreeSpace = -1;

    if(statfs("/var", &buf) >=0) {

        freeSpace = (unsignedlonglong)(buf.f_bsize* buf.f_bavail);

    }

    NSString*str = [NSStringstringWithFormat:@"手机剩余存储空间为:%0.2lld MB",freeSpace/1024/1024];

    returnstr;

}

或者

 floattotalsize =0.0;

    /// 剩余大小

    floatfreesize =0.0;

    /// 是否登录

    NSError*error =nil;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];

    if(dictionary)

    {

        NSNumber*_free = [dictionaryobjectForKey:NSFileSystemFreeSize];

        freesize = [_freeunsignedLongLongValue]*1.0/(1024);


        NSNumber*_total = [dictionaryobjectForKey:NSFileSystemSize];

        totalsize = [_totalunsignedLongLongValue]*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);

你可能感兴趣的:(获取手机存储空间)