美文网首页
iOS清理缓存(OC/Swift)

iOS清理缓存(OC/Swift)

作者: ray_1942 | 来源:发表于2017-02-16 20:02 被阅读0次

    OC版:

    - (void)clearCache{

    [SVProgressHUDshowWithStatus:@"清理缓存中..."maskType:SVProgressHUDMaskTypeBlack];

    NSString*crashSize =self.cachesSize;

    dispatch_async(

    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)

    , ^{

    NSString*cachPath =filePath;

    NSArray*files = [[NSFileManagerdefaultManager]subpathsAtPath:cachPath];

    for(NSString*pinfiles) {

    NSError*error;

    NSString*path = [cachPathstringByAppendingPathComponent:p];

    if([[NSFileManagerdefaultManager]fileExistsAtPath:path]) {

    [[NSFileManagerdefaultManager]removeItemAtPath:patherror:&error];

    }

    }

    dispatch_async(dispatch_get_main_queue(), ^{

    self.cachesSize= [CellFactorygetCacheSizeWithFilePath:filePath];

    [self.tableViewreloadData];

    });

    });

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

    [SVProgressHUDshowSuccessWithStatus:[NSStringstringWithFormat:@"清除了%@的缓存",crashSize]];

    [MobClickevent:mine_set_clearcrash];

    });

    }

    Swift版:

    /**清理缓存*/

    funcclearAction() {

    letalert =UIAlertController(title:"清除缓存", message:getCacheSize(), preferredStyle:UIAlertControllerStyle.alert)

    letalertConfirm =UIAlertAction(title:"确定", style:UIAlertActionStyle.default) { (alertConfirm) ->Voidin

    // 点击确定时开始删除

    forpinself.getCacheFilePath()!{

    // 拼接路径

    letpath =self.getCachePath()!.appendingFormat("/\(p)")

    // 判断是否可以删除

    if(FileManager.default.fileExists(atPath: path)){

    // 删除

    do{

    tryFileManager.default.removeItem(atPath: path)

    }catch{

    print(error)

    }

    }

    }

    }

    alert.addAction(alertConfirm)

    letcancle =UIAlertAction(title:"取消", style:UIAlertActionStyle.cancel) { (cancle) ->Voidin

    }

    alert.addAction(cancle)

    // 提示框弹出

    present(alert, animated:true) { () ->Voidin

    }

    }

    相关文章

      网友评论

          本文标题:iOS清理缓存(OC/Swift)

          本文链接:https://www.haomeiwen.com/subject/cfmqwttx.html