Krishna
Krishna

Reputation: 903

In Phonegap how to get free space of device

I am trying to make offline online app using cordova in Android, ios and Windows Platform. In this App there are 570MB data to store in database. So I need to first check available Memory space of device. But I got always 0. I am using this code. Give me any solution how I get free space in all Platform.

cordova.exec(
    function(freeSpace) {
      console.log('reported free space is ' + freeSpace);
    },
    function() {
      console.log('failed');
    }, 
    "File", "getFreeDiskSpace", []
);

Upvotes: 0

Views: 545

Answers (1)

Krishna
Krishna

Reputation: 903

I got solution for iOS. Update your CDVFile.m in your phonegap with this code:

- (void)getFreeDiskSpace:(CDVInvokedUrlCommand*)command
{    
    uint64_t totalSpace = 0;
    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 *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
        NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
        totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
        totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
        NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
    } else {
        //NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]);
        NSLog(@"Error Obtaining System Memory Info");
    }
    NSString* strFreeSpace = [NSString stringWithFormat:@"%llu", totalFreeSpace];
    CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:strFreeSpace];

    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];

    //return totalFreeSpace;

}

and then use this code in javascript file.

cordova.exec(
    function(freeSpace) {
      console.log('reported free space is ' + freeSpace);
    },
    function() {
      console.log('failed');
    }, 
    "File", "getFreeDiskSpace", []
);

Upvotes: 1

Related Questions