GlobalData.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // GlobalData.m
  3. // JianYuIOS
  4. //
  5. // Created by apple on 2019/1/14.
  6. // Copyright © 2019年 lixianglan. All rights reserved.
  7. //
  8. #import "GlobalData.h"
  9. @implementation GlobalData
  10. + (GlobalData *)shareInstance
  11. {
  12. static GlobalData *globalData = nil;
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. globalData = [[super allocWithZone:NULL] init];
  16. });
  17. return globalData;
  18. }
  19. - (instancetype)init
  20. {
  21. if (self = [super init])
  22. {
  23. [self setInitValues];
  24. }
  25. return self;
  26. }
  27. -(void)setInitValues
  28. {
  29. //初始化属性
  30. [self initShuXing];
  31. [self createPlist];
  32. }
  33. -(void)createPlist {
  34. NSString *filePath = [self getPlistPath];
  35. NSFileManager*fileManager = [NSFileManager defaultManager];
  36. if([fileManager fileExistsAtPath:filePath] ==NO) {
  37. NSLog(@"================plist不存在");
  38. NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init];
  39. [usersDic setValue:@[] forKey:@"jsonData"];
  40. [usersDic writeToFile:filePath atomically:YES];
  41. }else{
  42. NSLog(@"================plist存在");
  43. }
  44. NSLog(@"%@",filePath);
  45. }
  46. -(void)initShuXing
  47. {
  48. self.searchIsRed = NO;
  49. self.subIsRed = NO;
  50. self.boxIsRed = NO;
  51. self.mineIsRed = NO;
  52. self.isClickNotiStatus = NO;
  53. self.isSearchToLoad= NO;
  54. self.isSubToLoad= NO;
  55. self.isBoxToLoad= NO;
  56. self.isMineToLoad = NO;
  57. self.isReLogin = NO;
  58. self.isOpen = NO;
  59. self.isPasteLogin = NO;
  60. self.isSkipPath = @"";
  61. NSString *pushID = [[NSUserDefaults standardUserDefaults] objectForKey:GRPCPushID];
  62. if (!pushID) {
  63. self.rpcPushID = @"";
  64. }else {
  65. self.rpcPushID = pushID;
  66. }
  67. }
  68. -(NSString *)getPlistPath {
  69. NSArray *sandboxpath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  70. NSString *filePath = [[sandboxpath objectAtIndex:0] stringByAppendingPathComponent:@"sandbox.plist"];
  71. return filePath;
  72. }
  73. //获取plist所有数据
  74. -(NSMutableDictionary *)getPlistAllData {
  75. //获取路径
  76. NSMutableDictionary *searchdata = [[NSMutableDictionary alloc] initWithContentsOfFile:[self getPlistPath]];
  77. //获取数据
  78. return searchdata;
  79. }
  80. //获取plist数量
  81. -(NSInteger)getPlistCount {
  82. NSMutableDictionary *dict = [self getPlistAllData];
  83. if (dict==nil) {
  84. return 0;
  85. }
  86. return [dict[@"jsonData"] count];
  87. }
  88. //清空plist表
  89. -(void)clearPlistData {
  90. NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init];
  91. [usersDic setValue:@[] forKey:@"jsonData"];
  92. [usersDic writeToFile:[self getPlistPath] atomically:YES];
  93. }
  94. //插入数据
  95. -(void)addOnePlistData:(NSDictionary *)dict {
  96. NSMutableDictionary *rootDic = [self getPlistAllData];
  97. //处理数据
  98. NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc]initWithDictionary:dict];
  99. [tmpDict setValue:[UIDevice currentDevice].platformString forKey:@"phoneType"];//手机型号
  100. [tmpDict setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"appVersion"];//当前版本号
  101. [tmpDict setValue:[LXUserDefaults token] forKey:@"token"];//用户
  102. int tmpInt = [[self getNowTimeTimestamp] intValue];
  103. [tmpDict setValue:[NSNumber numberWithInt:tmpInt] forKey:@"operTime"];//当前时间戳
  104. [rootDic[@"jsonData"] addObject:tmpDict];
  105. [rootDic writeToFile:[self getPlistPath] atomically:YES];
  106. }
  107. -(NSString *)getNowTimeTimestamp {
  108. NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
  109. NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
  110. return timeSp;
  111. }
  112. #pragma mark ****缓存目录相关
  113. //返回缓存根目录 "caches"
  114. -(NSString *)getCachesDirectory
  115. {
  116. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  117. NSString *caches = [paths firstObject];
  118. return caches;
  119. }
  120. //返回根目录路径 "document"
  121. -(NSString *)getDocumentPath
  122. {
  123. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  124. NSString *documentPath = [paths firstObject];
  125. return documentPath;
  126. }
  127. -(NSString *)getfsCachedData {
  128. NSString *libraryDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
  129. NSUserDomainMask, YES)[0];
  130. NSString *bundleId = [[[NSBundle mainBundle] infoDictionary]
  131. objectForKey:@"CFBundleIdentifier"];
  132. NSString *webkitFolderInLib = [NSString stringWithFormat:@"%@/WebKit",libraryDir];
  133. NSString *webKitFolderInCaches = [NSString
  134. stringWithFormat:@"%@/Caches/%@/WebKit",libraryDir,bundleId];
  135. NSString *webKitFolderInCachesfs = [NSString
  136. stringWithFormat:@"%@/Caches/%@/fsCachedData",libraryDir,bundleId];
  137. return webKitFolderInCachesfs;
  138. // NSError *error;
  139. // /* iOS8.0 WebView Cache的存放路径 */
  140. // [[NSFileManager defaultManager] removeItemAtPath:webKitFolderInCaches error:&error];
  141. // [[NSFileManager defaultManager] removeItemAtPath:webkitFolderInLib error:nil];
  142. //
  143. // /* iOS7.0 WebView Cache的存放路径 */
  144. // [[NSFileManager defaultManager] removeItemAtPath:webKitFolderInCachesfs error:&error];
  145. }
  146. //创建文件目录
  147. -(BOOL)creatDir:(NSString*)dirPath
  148. {
  149. if ([[NSFileManager defaultManager] fileExistsAtPath:dirPath])//判断dirPath路径文件夹是否已存在,此处dirPath为需要新建的文件夹的绝对路径
  150. {
  151. return NO;
  152. }
  153. else
  154. {
  155. [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];//创建文件夹
  156. return YES;
  157. }
  158. }
  159. -(BOOL)deleteDir:(NSString*)dirPath
  160. {
  161. if([[NSFileManager defaultManager] fileExistsAtPath:dirPath]) {
  162. NSError *error=nil;
  163. return [[NSFileManager defaultManager] removeItemAtPath:dirPath error:&error];
  164. }
  165. return NO;
  166. //如果存在临时文件的配置文件
  167. }
  168. //移动文件夹
  169. -(BOOL)moveDir:(NSString*)srcPath to:(NSString*)desPath {
  170. NSError *error=nil;
  171. if([[NSFileManager defaultManager] moveItemAtPath:srcPath toPath:desPath error:&error]!=YES)// prePath 为原路径、 cenPath 为目标路径
  172. {
  173. NSLog(@"移动文件失败");
  174. return NO;
  175. }else{
  176. NSLog(@"移动文件成功");
  177. return YES;
  178. }
  179. }
  180. //创建文件
  181. -(BOOL)creatFile:(NSString*)filePath withData:(NSData*)data
  182. {
  183. return [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
  184. }
  185. //读取文件
  186. -(NSData*)readFile:(NSString *)filePath
  187. {
  188. return [NSData dataWithContentsOfFile:filePath options:0 error:NULL];
  189. }
  190. //删除文件
  191. -(BOOL)deleteFile:(NSString *)filePath {
  192. return [self deleteDir:filePath];
  193. }
  194. -(NSString *)getFilePath:(NSString *)fileName
  195. {
  196. NSString *dirPath = [[self getDocumentPath] stringByAppendingPathComponent:fileName];
  197. return dirPath;
  198. }
  199. -(BOOL)writeDataToFile:(NSString *)fileName data:(NSData *)data
  200. {
  201. NSString *filePath=[self getFilePath:fileName];
  202. return [self creatFile:filePath withData:data];
  203. }
  204. -(NSData*)readDataFromFile:(NSString*)fileName
  205. {
  206. NSString *filePath=[self getFilePath:fileName];
  207. return [self readFile:filePath];
  208. }
  209. @end