2013-03-05 22 views
0

在我的應用程序中,我想下載一個大數據文件(可能超過50MB),但同時如果用戶在音樂和其他東西中使用了大量空間,那麼代碼如何才能知道內存應用程序是有限的(例如40 MB),代碼現在應限制下載。如何檢查iOS中應用程序的可用空間?

+1

應用消耗iphone的內存,所以你之前打個電話下載的文件檢查設備內存:) – Rushabh 2013-03-05 11:03:07

+0

谷歌是你朋友。用它。 – occulus 2013-03-05 11:07:43

回答

2

您可以創建一個函數來做到這一點:

#include <sys/param.h> 
#include <sys/mount.h> 

+(float) diskSpaceLeft { 
    NSArray* thePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    struct statfs tStats; 
    const char *path = [[NSFileManager defaultManager] fileSystemRepresentationWithPath:[thePath lastObject]]; 
    statfs(path, &tStats);   
    float availableSpace = (float)(tStats.f_bavail * tStats.f_bsize);   
    return availableSpace; 
} 
0

使用本:

#include <sys/param.h> 
#include <sys/mount.h> 

+(float) diskSpaceAvailable { 
    NSArray* pathDocDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    struct statfs tStats; 
    statfs([[pathDocDir lastObject] cString], &tStats); 
    float total_space = (float)(tStats.f_blocks * tStats.f_bsize); 

    return total_space; 
}