HWSetViewController.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // HWSetViewController.m
  3. // Private-X
  4. //
  5. // Created by 余衡武 on 2022/3/24.
  6. //
  7. #import "HWSetViewController.h"
  8. #import "HWAgreementViewController.h"
  9. #import "HWAboutViewController.h"
  10. #import <WebKit/WebKit.h>
  11. @interface HWSetViewController ()
  12. @property (weak, nonatomic) IBOutlet UIView *bgViewOne;
  13. @property (weak, nonatomic) IBOutlet UIView *bgViewTwo;
  14. @property (weak, nonatomic) IBOutlet UIView *bgViewThree;
  15. @property (weak, nonatomic) IBOutlet UILabel *storeLabel;
  16. @property (weak, nonatomic) IBOutlet UIView *header;
  17. @end
  18. @implementation HWSetViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self.header mas_updateConstraints:^(MASConstraintMaker *make) {
  22. make.height.mas_equalTo(NAVIHEIGHT);
  23. }];
  24. [self.bgViewOne addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewOneDidClick)]];
  25. [self.bgViewTwo addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewTwoDidClick)]];
  26. [self.bgViewThree addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewThreeDidClick)]];
  27. self.bgViewOne.layer.cornerRadius = 10;
  28. self.bgViewOne.layer.masksToBounds = YES;
  29. self.bgViewTwo.layer.cornerRadius = 10;
  30. self.bgViewTwo.layer.masksToBounds = YES;
  31. self.bgViewThree.layer.cornerRadius = 10;
  32. self.bgViewThree.layer.masksToBounds = YES;
  33. [self.bgViewThree setHidden:YES];
  34. self.storeLabel.text = [self getCacheSize];
  35. }
  36. - (IBAction)backBtnClick:(UIButton *)sender {
  37. [self.navigationController popViewControllerAnimated:YES];
  38. }
  39. - (void)bgViewOneDidClick {
  40. HLog(@"清除缓存");
  41. [self toShowTips];
  42. }
  43. - (void)bgViewTwoDidClick {
  44. HLog(@"关于我们");
  45. HWAboutViewController *vc = [[HWAboutViewController alloc] init];
  46. [self.navigationController pushViewController:vc animated:YES];
  47. }
  48. - (void)bgViewThreeDidClick {
  49. HLog(@"隐私政策");
  50. HWAgreementViewController *vc = [[HWAgreementViewController alloc] init];
  51. [self.navigationController pushViewController:vc animated:YES];
  52. }
  53. - (void)toShowTips {
  54. NSString *tipsStr = NSLocalizedString(@"app_set_clear_cache",nil);
  55. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"other_confirm",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  56. [self clearWebViewCache];
  57. }];
  58. [sureAction setValue:[UIColor hwColor:@"#3B7FFF"] forKey:@"titleTextColor"];//iOS8.3
  59. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"other_cancel",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  60. }];
  61. [cancelAction setValue:[UIColor colorWithHexString:@"#FFFFFF" alpha:0.5] forKey:@"titleTextColor"];//iOS8.3
  62. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:tipsStr message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
  63. alertVC.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
  64. [alertVC addAction:sureAction];
  65. [alertVC addAction:cancelAction];
  66. [self presentViewController:alertVC animated:YES completion:^{
  67. }];
  68. }
  69. - (NSString *)getCacheSize {
  70. /* 取得Library文件夹的位置*/
  71. NSString *libraryDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES)[0];
  72. /* 取得bundle id,用作文件拼接用*/
  73. NSString *bundleId = [[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleIdentifier"];
  74. /*
  75. * 拼接缓存地址,具体目录为App/Library/Caches/你的APPBundleID/fsCachedData
  76. */
  77. NSString *webKitFolderInCachesfs = [NSString stringWithFormat:@"%@/Caches/%@/fsCachedData",libraryDir,bundleId];
  78. NSError *error;
  79. /* 取得目录下所有的文件,取得文件数组*/
  80. NSFileManager *fileManager = [NSFileManager defaultManager];
  81. // NSArray *fileList = [[NSArray alloc] init];
  82. //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
  83. NSArray *fileList = [fileManager contentsOfDirectoryAtPath:webKitFolderInCachesfs error:&error];
  84. NSInteger fileSize = 0;
  85. /* 遍历文件组成的数组*/
  86. for(NSString *fileName in fileList){
  87. /* 定位每个文件的位置*/
  88. NSString * path = [[NSBundle bundleWithPath:webKitFolderInCachesfs] pathForResource:fileName ofType:@""];
  89. /* 将文件转换为NSData类型的数据*/
  90. NSData *fileData = [NSData dataWithContentsOfFile:path];
  91. /* 如果FileData的长度大于2,说明FileData不为空*/
  92. if(fileData.length >2){
  93. /* 创建两个用于显示文件类型的变量*/
  94. int char1 =0;
  95. int char2 =0;
  96. [fileData getBytes:&char1 range:NSMakeRange(0,1)];
  97. [fileData getBytes:&char2 range:NSMakeRange(1,1)];
  98. /* 拼接两个变量*/
  99. NSString *numStr = [NSString stringWithFormat:@"%i%i",char1,char2];
  100. /* 如果该文件前四个字符是6033,说明是Html文件,删除掉本地的缓存*/
  101. if([numStr isEqualToString:@"6033"]){
  102. // [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@",webKitFolderInCachesfs,fileName]error:&error];
  103. fileSize += fileData.length;
  104. continue;
  105. }
  106. }
  107. }
  108. return [iTools getFileSizeStringFormedValue:fileSize];
  109. }
  110. - (void)clearWebViewCache {
  111. NSArray * types = @[WKWebsiteDataTypeMemoryCache,WKWebsiteDataTypeDiskCache]; // 9.0之后才有的
  112. NSSet *websiteDataTypes = [NSSet setWithArray:types];
  113. NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
  114. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
  115. HLog(@"缓存清理完成");
  116. self.storeLabel.text = @"0MB";
  117. [[iToast makeText:@"缓存清理完成"] show];
  118. }];
  119. // /* 取得Library文件夹的位置*/
  120. // NSString *libraryDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES)[0];
  121. // /* 取得bundle id,用作文件拼接用*/
  122. // NSString *bundleId = [[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleIdentifier"];
  123. // /*
  124. // * 拼接缓存地址,具体目录为App/Library/Caches/你的APPBundleID/fsCachedData
  125. // */
  126. // NSString *webKitFolderInCachesfs = [NSString stringWithFormat:@"%@/Caches/%@/fsCachedData",libraryDir,bundleId];
  127. //
  128. // NSError *error;
  129. // /* 取得目录下所有的文件,取得文件数组*/
  130. // NSFileManager *fileManager = [NSFileManager defaultManager];
  131. //// NSArray *fileList = [[NSArray alloc] init];
  132. // //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
  133. // NSArray *fileList = [fileManager contentsOfDirectoryAtPath:webKitFolderInCachesfs error:&error];
  134. // /* 遍历文件组成的数组*/
  135. // for(NSString * fileName in fileList){
  136. // /* 定位每个文件的位置*/
  137. // NSString * path = [[NSBundle bundleWithPath:webKitFolderInCachesfs] pathForResource:fileName ofType:@""];
  138. // /* 将文件转换为NSData类型的数据*/
  139. // NSData * fileData = [NSData dataWithContentsOfFile:path];
  140. // /* 如果FileData的长度大于2,说明FileData不为空*/
  141. // if(fileData.length >2){
  142. // /* 创建两个用于显示文件类型的变量*/
  143. // int char1 =0;
  144. // int char2 =0;
  145. //
  146. // [fileData getBytes:&char1 range:NSMakeRange(0,1)];
  147. // [fileData getBytes:&char2 range:NSMakeRange(1,1)];
  148. // /* 拼接两个变量*/
  149. // NSString *numStr = [NSString stringWithFormat:@"%i%i",char1,char2];
  150. // /* 如果该文件前四个字符是6033,说明是Html文件,删除掉本地的缓存*/
  151. // if([numStr isEqualToString:@"6033"]){
  152. // [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@",webKitFolderInCachesfs,fileName]error:&error];
  153. // continue;
  154. // }
  155. // }
  156. // }
  157. //
  158. // self.storeLabel.text = [self getCacheSize];
  159. // [[iToast makeText:@"缓存清理完成"] show];
  160. }
  161. @end