2 questions about - iOS current application store

I read a lot of information about this, did some tests and got the wrong results, and I feel like I'm missing something. The only information I receive and its correct information: free space / total space DEVICE.

But I only need information about my APP. So:

  • How to get the storage size of iOS applications. which are displayed on the settings. (not other applications, current application)
  • How to delete all cached / tmp / other files, so the storage size will be the same size as during the first installation?

Thanks.

+5
source share
3 answers

For the first question, the code below will work. But I don't know if this method is consistent with using the 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 calculates the size of the entire packet. Part of the loan goes to 1218GG on this stack issue . But its method is not taken into account in binary format (iOS uses the binary system, Mac OS 10.6 and later uses the base system 10. See this article ) and the size of sandbox files is not taken into account. I have changed / added the appropriate code.

This compares the size of the iOS application for files, and displays one of them (Finder Display vs. App Calculation):

61.801 B vs 61 KB; 668 KB (637,472 V) versus 623 KB; 13.5 MB (13.520.085 B) vs 13.4 MB

There are several options. Perhaps because of the differences between how Mac OS calculates file size and iOS calculates file size. On iPhone runnig iOS 9, the system shows 204 KB, and my method shows 208 KB. When the size of the application exceeds 10 MB, the variation may be ignored.

0
source

You can use this to find cached documents in your application and then delete them:

 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); 
-1
source

(please read the full text) hey bro to check the size of a specific application just follow these steps 1-open settings 2 shared 3x storage and using icloud here you see two things: 1-storage and 2 icloud both have 3 options 1 used- 2 available- 3 - you just click on the storage management from the storage here, you will see all the used and available storage and if you check the size of a specific application tape on it, you will see the size and data size of this application, as a rule, you check e the size of the application from the application store before installation. There is also the opportunity to delete the application, if you click on it, you will delete the application, as well as delete it.

-4
source

Source: https://habr.com/ru/post/1259900/


All Articles