美文网首页
获取手机存储空间

获取手机存储空间

作者: 今年27 | 来源:发表于2018-04-03 11:22 被阅读7次

    #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);

    相关文章

      网友评论

          本文标题:获取手机存储空间

          本文链接:https://www.haomeiwen.com/subject/aveqhftx.html