iOSのクリーンアップキャッシュ

5314 ワード

目次を読む
  • 1、Cachesディレクトリのキャッシュ1
  • 2、Cacheディレクトリのキャッシュ二
  • 3、NSUserDefaults(軽量レベルのローカルデータを格納するのに適している)、ストレージファイルのクリーンアップ
  • 開発者として,キャッシュのクリーンアップも当然必要である.今回はiOSでのキャッシュのクリーンアップ方法について簡単にお話しします.
    キャッシュのクリーンアップは、通常、この3つの方法で行われます.
    (1)項目のキャッシュクリアボタン
    (2)appボタンをクリックしてキャッシュをクリアする
    (3)手動でプロセスを殺す(説明:アップルの携帯電話を使うとき、ほとんどの人は毎回appボタンをクリックするのが好きではありません.そのため、お客様は手動でプロセスを殺すときに、appをキャッシュして整理する要求があります)
    次に、iOSのクリーンアップキャッシュを3つの側面から分析します.
    iOSアプリケーションはサンドボックス(sandbox)にあり、ファイルの読み書き権限が制限されており、いくつかのディレクトリでのみファイルを読み書きできます.
  • Documents:アプリケーション内のユーザーデータはここに置くことができます.iTunesのバックアップとリカバリには、このディレクトリ
  • が含まれます.
  • tmp:一時ファイルを保存し、iTunesはこのディレクトリをバックアップおよびリカバリしません.このディレクトリの下のファイルは、アプリケーションが終了すると
  • を削除する可能性があります.
  • Library/Caches:キャッシュファイルを保存し、iTunesはこのディレクトリをバックアップしません.このディレクトリの下のファイルはアプリケーションが終了して
  • を削除しません.
    プロジェクト内のキャッシュのクリーンアップボタンのコードはリストされません(ビューにButtonを直接追加したり、tableViewにcellをリストしてキャッシュのクリーンアップボタンを作成したりすることができます)、次にキャッシュのクリーンアップコードを直接与えます.
    トップに戻る

    1、Cachesディレクトリのキャッシュ一

    #pragma mark - ************* Get cache size( )*************
    - (NSString *)getCacheSize{
        NSFileManager *fileManager = [NSFileManager defaultManager];
     
        NSArray *paths = nil;
        paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSMutableString *cachepath = [paths objectAtIndex:0];
     
        NSError *error = nil;
        // 
        float cacheSize = 0.0f;
        //fileList 
        NSArray *fileList = [fileManager contentsOfDirectoryAtPath:cachepath error:&error];
        BOOL isDir = NO;
     
        // fileList 
        for (NSString *file in fileList) {
            NSString *path = [cachepath stringByAppendingPathComponent:file];
            [fileManager fileExistsAtPath:path isDirectory:(&isDir)];
            // 
            NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];//[[NSFileManager defaultManager] fileAttributesAtPath: path traverseLink: YES];
            // 
            NSDate *modificationDate = (NSDate*)[fileAttributes objectForKey: NSFileModificationDate];
     
            int timeSepearte = (int)[[NSDate date] timeIntervalSince1970]-(int)[modificationDate timeIntervalSince1970];
            if (timeSepearte>(3*86400)) {
                if ([fileManager isDeletableFileAtPath:path]) {
                    [fileManager removeItemAtPath:path error:nil];
                }
            }else{
                if (isDir) {
                    cacheSize = cacheSize + [self fileSizeForDir:path];
                }
            }
            isDir = NO;
        }
        NSString *cacheSizeString = @"";
        if (cacheSize >1024*1024) {
            float cacheSize_M = cacheSize/(1024*1024);
            cacheSizeString = [NSString stringWithFormat:@"%.1f M",cacheSize_M];
        }else if (cacheSize>1024&&cacheSize<1024*1024) {
            float cacheSize_KB = cacheSize/(1024);
            cacheSizeString = [NSString stringWithFormat:@"%.1f KB",cacheSize_KB];
        }else{
            float cacheSize_BYT = cacheSize/(1024);
            cacheSizeString = [NSString stringWithFormat:@"%.1f B",cacheSize_BYT];
        }
        
        return cacheSizeString;
    }
     
    -(float)fileSizeForDir:(NSString*)path// 
    {
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        float size =0;
        NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];
        for(int i = 0; i

    2、Cacheディレクトリのキャッシュ二
    #pragma mark -  
    - (NSString *)getCacheSize1
    {
        // 
         long long cacheSize = 0;
         //01. 
        NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
        //02. 
         NSFileManager *filemanager = [NSFileManager defaultManager];
     
             // 
         NSArray *subPaths = [filemanager subpathsOfDirectoryAtPath:cacheFilePath error:nil];
     
            // 
        for (NSString *subPath in subPaths) {
                     //1). 
                 NSString *filePath = [cacheFilePath stringByAppendingFormat:@"/%@",subPath];
                    //2). 
                 long long fileSize = [[filemanager attributesOfItemAtPath:filePath error:nil]fileSize];
                     //3). 
                 cacheSize += fileSize;
             }
         float size_m = cacheSize/(1024*1024);
         return [NSString stringWithFormat:@"%.2fM",size_m];
        
    }
    

    キャッシュのクリーンアップ:
    - (void)cleanCache
    {
        // 
        NSFileManager *manager = [NSFileManager defaultManager];
        NSArray *paths = nil;
        paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSMutableString *cachepath = [paths objectAtIndex:0];
        NSError *error = nil;
        NSArray *fileList = [manager contentsOfDirectoryAtPath:cachepath error:&error];
        for (NSString *file in fileList) {
            NSString *path = [cachepath stringByAppendingPathComponent:file];
            if ([manager isDeletableFileAtPath:path]) {
                [manager removeItemAtPath:path error:nil];
            }
        }
     
    }
    

    3、NSUserDefaults(軽量レベルのローカルデータを保存するのに適している)、ストレージファイルの整理
    - (void)cleanCache
    {
        NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];
        NSDictionary *dictionary = [defatluts dictionaryRepresentation];
        for(NSString *key in [dictionary allKeys]){
            [defatluts removeObjectForKey:key];
            [defatluts synchronize];
        }
    }