본문 바로가기
ios 뽀개기/objective-c

ios 저장 용량 계산

by 인생여희 2018. 12. 27.
반응형

ios 저장 용량 계산



#  pragma about Storage Method


//전체 저장 용량

-(uint64_t)getTotalDisk {

    uint64_t totalSpace = 0;

    

    NSError *error = nil;

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

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

    

    if (dictionary) {

        NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];

        

        totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];

        

        NSLog(@"Memory Capacity of %llu GB  memory available.", ((totalSpace/1024ll)/1024ll)/1024ll);

    } else {

        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);

    }

    

    return(((totalSpace/1024ll)/1024ll)/1024ll);

}



//저장할 수 있는 용량

-(uint64_t)getFreeDisk {

    

    uint64_t totalFreeSpace = 0;

    NSError *error = nil;

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

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

    

    if (dictionary) {

        

        NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];

        

        totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];

        NSLog(@"Memory Capacity %llu GB Free memory available.", (((totalFreeSpace/1024ll)/1024ll)/1024ll));

    } else {

        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);

    }

    

    return (((totalFreeSpace/1024ll)/1024ll)/1024ll);



}



위의 함수 호출하는곳

//앨범 용량 체크 후 json 값으로 리턴

-(NSString *) getStorage{

   

    NSString *totalDisk = [NSString stringWithFormat:@"%llu", (unsigned long long) [self getTotalDisk]];

    NSString *freeDisk = [NSString stringWithFormat:@"%llu", (unsigned long long) [self getFreeDisk]];

    

    

     NSDictionary *contentDictionary = [NSDictionary dictionaryWithObjectsAndKeys:totalDisk, @"Total_Storage", freeDisk, @"Free_Storage", nil];

    

    NSData *data = [NSJSONSerialization dataWithJSONObject:contentDictionary options:NSJSONWritingPrettyPrinted error:nil];

    NSString *jsonStr = [[NSString alloc] initWithData:data

                                              encoding:NSUTF8StringEncoding];

    

    return jsonStr;


}


반응형

댓글