2016-11-17 66 views
5

我讀了很多關於它的信息,做了一些測試並得到錯誤的結果,我覺得我錯過了一些東西。 我得到的唯一信息,它是正確的信息: DEVICE可用空間/總空間。關於 - ios當前應用程序存儲的2個問題

但我只需要關於我的APP的信息。所以:

  1. 如何獲取iOS應用程序的存儲大小。顯示在設置上的 。 (不是其他應用程序,當前應用程序)
  2. 如何刪除所有緩存/ tmp /其他文件,因此存儲大小將與第一次安裝時的大小相同?

謝謝。

+0

iOS應用程序擁有自己的沙盒並寫入他們自己的文檔目錄。只需刪除應用程序文檔目錄的內容即可清除所有內容。 「應用程序存儲大小」是什麼意思? – NSNoob

+0

當您轉到設置應用程序,存儲和iCloud使用情況時,您會看到應用程序列表和存儲大小。 我想獲得代碼,我的應用程序存儲大小的值。 關於您在文檔目錄上所說的內容,我不確定是否足夠。 的含義 - 如果我刪除文檔文件夾,應用程序存儲大小將不會與首次安裝(甚至關閉)相同。還有緩存文件夾等。 這就是爲什麼我在這裏問,也許有人知道要刪除的文件夾的完整列表。 – user1105951

+0

用於清除您可以使用folderPath的所有數據。按照http://stackoverflow.com/a/9358551/5215474 – Saranjith

回答

-1

您可以使用此找到您的應用程序緩存文件,然後刪除它們:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
NSString *firstPath = paths[0]; 
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:firstPath error:nil]; 
NSEnumerator *contentsEnumurator = [contents objectEnumerator];   
NSString *file; 

unsigned long long int folderSize = 0; 

while (file = [contentsEnumurator nextObject]) { 

    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[firstPath stringByAppendingPathComponent:file] error:nil]; 
    folderSize += [[fileAttributes objectForKey:NSFileSize] intValue]; 

} 

folderSize = folderSize/1000.0f/1000.0f; // Folder size in MB (more pressition folderSize/1024.0f/1024.0f); 

NSMutableArray *filesToRemove = [[NSMutableArray alloc] init]; 
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:firstPath error:NULL]; 

for (NSString *path in files) { 

    BOOL isDir; 
    NSString *filePath = [[[self class] cachesDirectoryPath] stringByAppendingPathComponent:path]; 
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir]; 

    if (fileExists && !isDir) { 

     NSString *filePath = [NSString stringWithFormat:@"%@/%@", firstPath ,path]; 
     NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath: filePath error:NULL]; 
     NSDictionary *fileDescription = [[NSDictionary alloc] init]; 

     @try { 
      //Adds the file description to the files to remove 
      fileDescription = @{ 
           @"kFilePath": filePath, 
           @"kFileSize": [attr objectForKey:NSFileSize], 
           @"kFileModificationDate": [attr objectForKey:NSFileModificationDate] 
           }; 
     } @catch (NSException *exception) { 
      NSLog(@"File description problem"); 
     } 

     if (fileDescription.allKeys.count > 0) { 
      [filesToRemove addObject:fileDescription]; 
     } 
    } 
} 
float removedMB = 0; 

for (NSDictionary *fileDescription in filesToRemove) { 
    NSLog(@"Removing file: %@", [fileDescription objectForKey:@"kFilePath"]); 
    removedMB += [((NSNumber *)[fileDescription objectForKey:@"kFileSize"]) floatValue]/1000.0f/1000.0f; 
    NSError *error = nil; 
    [[NSFileManager defaultManager] removeItemAtPath:[fileDescription objectForKey:@"kFilePath"] error:&error]; 
} 

NSLog(@"Removed MB: %f", removedMB); 
-4

(請閱讀全文)嘿老兄用於檢查特定應用程序的大小,只要按照下列步驟 1,打開設置 2,然後一般 3抽頭存儲& icloud的使用 這裏你看到的兩件事情1的存儲和2- icloud的都有3個選項 1 used- 2服務現有 3管理存儲 - 你只需輕按上管理存儲從存儲在這裏你會se e所有已用和可用的存儲空間 ,如果您檢查其上的特定應用程序磁帶的大小,則會看到此應用程序的大小和數據大小,通常您會在安裝之前從應用商店檢查應用程序的大小。 這裏也有選擇刪除應用程序,如果你點擊它,你將刪除應用程序以及它的數據也刪除

+0

嘿兄弟...那是什麼? – holex

+0

你是什麼意思 –

+0

這不是OP的問題的答案。 – holex

0

對於第一個問題,下面的代碼將工作。但我不知道這種方法是否符合Apple的API使用。

// Get block size on user partition. 
struct statfs *mntbufp = NULL; 
getmntinfo(&mntbufp, 0); 

int i, count = 0; 
unsigned int blockSize; 

count = getmntinfo(&mntbufp, 0); 

for (i = 0; i < count; ++i) 
{ 
    // Find users partition. 
    if (strcmp(mntbufp[i].f_mntonname, "/private/var") == 0) { 
     blockSize = mntbufp[i].f_bsize; 
     break; 
    } 
} 

//get full pathname of bundle directory 
NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; 

//get paths of all of the contained subdirectories 
NSArray *bundleArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:bundlePath error:nil]; 

//to access each object in array 
NSEnumerator *filesEnumerator = [bundleArray objectEnumerator]; 

NSString *fileName; 

unsigned long long int fileSize = 0; 
unsigned long long int filesSize = 0; 
unsigned long long int sizeOnDisk = 0; 

NSError *error = nil; 

//return next object from enumerator 
while (fileName = [filesEnumerator nextObject]) { 
    NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:[bundlePath stringByAppendingPathComponent:fileName] error:&error]; 

    fileSize = [fileDictionary fileSize]; 

    // File size is sum of all file sizes. 
    filesSize += fileSize; 

    // Calculate size on disk. 
    sizeOnDisk += ceil((double)fileSize/blockSize) * blockSize; 
} 

// Find sandbox path. 
NSString *sandboxPath = NSHomeDirectory(); 
NSArray *sandboxArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:sandboxPath error:nil]; 
filesEnumerator = [sandboxArray objectEnumerator]; 

while (fileName = [filesEnumerator nextObject]) { 
    NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:[sandboxPath stringByAppendingPathComponent:fileName] error:&error]; 

    fileSize = [fileDictionary fileSize]; 

    filesSize += fileSize; 
    sizeOnDisk += ceil((double)fileSize/blockSize) * blockSize; 
} 

//converts a byte count value into a textual representation that is formatted with the appropriate byte modifier (KB, MB, GB and so on) 
NSString *fileSizeStr = [NSByteCountFormatter stringFromByteCount:filesSize countStyle:NSByteCountFormatterCountStyleBinary]; 
NSString *sizeOnDiskStr = [NSByteCountFormatter stringFromByteCount:sizeOnDisk countStyle:NSByteCountFormatterCountStyleBinary];` 

這會計算整個包的大小。部分功勞歸功於this stackoverflow question中的1218GG。但他的方法不以二進制格式計數(iOS使用二進制系統; Mac OS 10.6和更高版本使用基本10系統,請參閱this article),並且不計算沙盒文件的大小。我修改/添加了相應的代碼。

這裏是文件大小iOS應用計算和所述一個Mac的取景器顯示之間的比較(取景器顯示與應用程序計算):

61801乙與61 KB; 668 KB(637,472 B)與623 KB; 13.5 MB(13,520,085 B)與13.4 MB

有一些變化。 可能由於Mac OS計算文件大小的方式和iOS計算文件大小之間的差異。在iPhone上運行iOS 9時,系統顯示204 KB,而我的方法計數爲208 KB。當應用尺寸大於10 MB時,可以忽略該差異。

+0

看來,iOS系統顯示的是「磁盤大小」而不是「文件大小」。所以文件大小和sizeOnDisk之間的轉換是必要的。目前的應用程序存儲是'sizeOnDisk'。 –

相關問題