123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- //
- // GlobalData.m
- // JianYuIOS
- //
- // Created by apple on 2019/1/14.
- // Copyright © 2019年 lixianglan. All rights reserved.
- //
- #import "GlobalData.h"
- @implementation GlobalData
- + (GlobalData *)shareInstance
- {
- static GlobalData *globalData = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- globalData = [[super allocWithZone:NULL] init];
- });
- return globalData;
- }
- - (instancetype)init
- {
- if (self = [super init])
- {
- [self setInitValues];
- }
- return self;
-
-
-
- }
- -(void)setInitValues
- {
- //初始化属性
- [self initShuXing];
- [self createPlist];
- }
- -(void)createPlist {
- NSString *filePath = [self getPlistPath];
- NSFileManager*fileManager = [NSFileManager defaultManager];
- if([fileManager fileExistsAtPath:filePath] ==NO) {
- NSLog(@"================plist不存在");
- NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init];
- [usersDic setValue:@[] forKey:@"jsonData"];
- [usersDic writeToFile:filePath atomically:YES];
- }else{
- NSLog(@"================plist存在");
- }
- NSLog(@"%@",filePath);
- }
- -(void)initShuXing
- {
- self.searchIsRed = NO;
- self.subIsRed = NO;
- self.boxIsRed = NO;
- self.mineIsRed = NO;
-
- self.isClickNotiStatus = NO;
- self.isSearchToLoad= NO;
- self.isSubToLoad= NO;
- self.isBoxToLoad= NO;
- self.isMineToLoad = NO;
-
- self.isReLogin = NO;
- self.isOpen = NO;
- self.isPasteLogin = NO;
- self.isSkipPath = @"";
- NSString *pushID = [[NSUserDefaults standardUserDefaults] objectForKey:GRPCPushID];
- if (!pushID) {
- self.rpcPushID = @"";
- }else {
- self.rpcPushID = pushID;
- }
-
- }
- -(NSString *)getPlistPath {
- NSArray *sandboxpath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *filePath = [[sandboxpath objectAtIndex:0] stringByAppendingPathComponent:@"sandbox.plist"];
- return filePath;
- }
- //获取plist所有数据
- -(NSMutableDictionary *)getPlistAllData {
- //获取路径
- NSMutableDictionary *searchdata = [[NSMutableDictionary alloc] initWithContentsOfFile:[self getPlistPath]];
- //获取数据
- return searchdata;
- }
- //获取plist数量
- -(NSInteger)getPlistCount {
- NSMutableDictionary *dict = [self getPlistAllData];
- if (dict==nil) {
- return 0;
- }
- return [dict[@"jsonData"] count];
- }
- //清空plist表
- -(void)clearPlistData {
- NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init];
- [usersDic setValue:@[] forKey:@"jsonData"];
- [usersDic writeToFile:[self getPlistPath] atomically:YES];
- }
- //插入数据
- -(void)addOnePlistData:(NSDictionary *)dict {
- NSMutableDictionary *rootDic = [self getPlistAllData];
- //处理数据
- NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc]initWithDictionary:dict];
- [tmpDict setValue:[UIDevice currentDevice].platformString forKey:@"phoneType"];//手机型号
- [tmpDict setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"appVersion"];//当前版本号
- [tmpDict setValue:[LXUserDefaults token] forKey:@"token"];//用户
- int tmpInt = [[self getNowTimeTimestamp] intValue];
- [tmpDict setValue:[NSNumber numberWithInt:tmpInt] forKey:@"operTime"];//当前时间戳
- [rootDic[@"jsonData"] addObject:tmpDict];
- [rootDic writeToFile:[self getPlistPath] atomically:YES];
- }
- -(NSString *)getNowTimeTimestamp {
- NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
- NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
- return timeSp;
- }
- #pragma mark ****缓存目录相关
- //返回缓存根目录 "caches"
- -(NSString *)getCachesDirectory
- {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *caches = [paths firstObject];
- return caches;
- }
- //返回根目录路径 "document"
- -(NSString *)getDocumentPath
- {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentPath = [paths firstObject];
- return documentPath;
- }
- -(NSString *)getfsCachedData {
- NSString *libraryDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
- NSUserDomainMask, YES)[0];
- NSString *bundleId = [[[NSBundle mainBundle] infoDictionary]
- objectForKey:@"CFBundleIdentifier"];
- NSString *webkitFolderInLib = [NSString stringWithFormat:@"%@/WebKit",libraryDir];
- NSString *webKitFolderInCaches = [NSString
- stringWithFormat:@"%@/Caches/%@/WebKit",libraryDir,bundleId];
- NSString *webKitFolderInCachesfs = [NSString
- stringWithFormat:@"%@/Caches/%@/fsCachedData",libraryDir,bundleId];
- return webKitFolderInCachesfs;
- // NSError *error;
- // /* iOS8.0 WebView Cache的存放路径 */
- // [[NSFileManager defaultManager] removeItemAtPath:webKitFolderInCaches error:&error];
- // [[NSFileManager defaultManager] removeItemAtPath:webkitFolderInLib error:nil];
- //
- // /* iOS7.0 WebView Cache的存放路径 */
- // [[NSFileManager defaultManager] removeItemAtPath:webKitFolderInCachesfs error:&error];
- }
- //创建文件目录
- -(BOOL)creatDir:(NSString*)dirPath
- {
- if ([[NSFileManager defaultManager] fileExistsAtPath:dirPath])//判断dirPath路径文件夹是否已存在,此处dirPath为需要新建的文件夹的绝对路径
- {
- return NO;
- }
- else
- {
- [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];//创建文件夹
- return YES;
- }
- }
- -(BOOL)deleteDir:(NSString*)dirPath
- {
- if([[NSFileManager defaultManager] fileExistsAtPath:dirPath]) {
- NSError *error=nil;
- return [[NSFileManager defaultManager] removeItemAtPath:dirPath error:&error];
- }
- return NO;
- //如果存在临时文件的配置文件
- }
- //移动文件夹
- -(BOOL)moveDir:(NSString*)srcPath to:(NSString*)desPath {
- NSError *error=nil;
- if([[NSFileManager defaultManager] moveItemAtPath:srcPath toPath:desPath error:&error]!=YES)// prePath 为原路径、 cenPath 为目标路径
- {
- NSLog(@"移动文件失败");
- return NO;
- }else{
- NSLog(@"移动文件成功");
- return YES;
- }
- }
- //创建文件
- -(BOOL)creatFile:(NSString*)filePath withData:(NSData*)data
- {
- return [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
- }
- //读取文件
- -(NSData*)readFile:(NSString *)filePath
- {
- return [NSData dataWithContentsOfFile:filePath options:0 error:NULL];
- }
- //删除文件
- -(BOOL)deleteFile:(NSString *)filePath {
- return [self deleteDir:filePath];
- }
- -(NSString *)getFilePath:(NSString *)fileName
- {
- NSString *dirPath = [[self getDocumentPath] stringByAppendingPathComponent:fileName];
- return dirPath;
- }
- -(BOOL)writeDataToFile:(NSString *)fileName data:(NSData *)data
- {
- NSString *filePath=[self getFilePath:fileName];
- return [self creatFile:filePath withData:data];
- }
- -(NSData*)readDataFromFile:(NSString*)fileName
- {
- NSString *filePath=[self getFilePath:fileName];
- return [self readFile:filePath];
- }
- @end
|