Michael Jajou
Michael Jajou

Reputation: 392

Check if there is enough free space on users phone to record a video

In my video camera app, I would like to check if there is enough storage on a users phone to record a video before allowing them to record. Just like youtube.

This is what happens when you do it on YouTube

enter image description here

How can I

1) Check if a user has enough free space on his/her phone before recording a video.

2) Show the user a warning if they do not have enough free space on their phone.

Thank you

Upvotes: 2

Views: 2311

Answers (1)

omanosoft
omanosoft

Reputation: 4339

I needed similar functionality, so I created function that returns number of free MB. I tested what happens when space is filled and found out that iOS starts showing notifications about no space left after about 120MB, and when that happen Camera stops working. But my app could save other files until it was reached about 30MB of free space. So to be sure, it is recommended that when you get value from this function, check it if it is at least 200(MB) and then allow user to save. I tested both functions(for Swift 5.0 and Objective C) in Xcode 11.0 on iPhone X and 6s and they worked very well.

In Swift 5.0, also works with lower Swift versions(with slight changes depending on version)

func getFreeSpace() -> CLongLong // return free space in MB
    {
        var totalFreeSpaceInBytes: CLongLong = 0; //total free space in bytes

        do{
            let spaceFree: CLongLong = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory())[FileAttributeKey.systemFreeSize] as! CLongLong;
            totalFreeSpaceInBytes = spaceFree;

        }catch let error{ // Catch error that may be thrown by FileManager
            print("Error is ", error);
        }

        let totalBytes: CLongLong = 1 * CLongLong(totalFreeSpaceInBytes);
        let totalMb: CLongLong = totalBytes / (1024 * 1024);

        return totalMb;
    }

In Objective C:

-(uint64_t)getFreeDiskspace { // return free space in MB
    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];
        NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
        uint64_t totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
        uint64_t totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
        NSLog(@"Memory Capacity: %llu MiB , Free: %llu MiB", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
        return ((totalFreeSpace/1024ll)/1024ll);
    } else {
        NSLog(@"Error getting memory info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
    }

    return 0;
}

Upvotes: 4

Related Questions