// // SettingController.m // BuWanVideo2.0 // // Created by weikou2016 on 16/8/10. // Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved. // #import "SettingController.h" #import "StorageSpaceTableViewCell.h" #import "SettingWebView.h" #import "UIView+Toast.h" #define indentfly @"cell" #define indentfly1 @"Mcell" #define indentfly2 @"WIFIcell" #define indentfly_logout @"LogoutCell" #define indentfly_unregister @"UnregisterCell" @interface SettingController(){ BOOL _userOnLine; } @property (nonatomic, strong) UITableView *mytableview; @property (nonatomic , strong) NSMutableArray *dataAry;//数据源 @end @implementation SettingController - (void)viewWillAppear:(BOOL)animated { _userOnLine=[[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"]; if(_mytableview){ [_mytableview reloadData]; } [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:NO animated:animated]; } - (void)viewDidLoad { [super viewDidLoad]; [self loadData]; } -(void)loadData{ self.navigationItem.title = @"设置"; self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor blackColor]}; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[[UIImage imageNamed:@"详情页面返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]; [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0, 32, 32); UIBarButtonItem *iconBarItem=[[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = iconBarItem; _dataAry = [[NSMutableArray alloc] initWithObjects:@[@"清空图片缓存"],@[@"手机存储"],@[@"常见问题",@"关于我们",@"免责声明",@"恢复购买"], nil]; if(!_mytableview){ _mytableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH) style:UITableViewStyleGrouped]; _mytableview.delegate = self; _mytableview.dataSource = self; [self.view addSubview:_mytableview]; _mytableview.backgroundColor = kGlobalBackgroundColor; _userOnLine=[[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"]; } //注册自定制的两个cell(非WiFi下禁止自动下载/手机存储) [_mytableview registerNib:[UINib nibWithNibName:@"StorageSpaceTableViewCell" bundle:nil] forCellReuseIdentifier:indentfly1]; } /** * 总的存储空间 * * @return 手机总的字节数 */ -(NSNumber *)totalDiskSpace{ NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil]; return [fattributes objectForKey:NSFileSystemSize]; } /** * 剩余存储空间 * * @return 手机剩余字节数 */ -(NSNumber *)freeDiskSpace{ NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil]; return [fattributes objectForKey:NSFileSystemFreeSize]; } /** * 用于退出登录 */ -(void)OutOfLogin{ [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"buUserInfo"]; [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"userOnLine"]; [[NSUserDefaults standardUserDefaults] synchronize]; [[NSNotificationCenter defaultCenter] postNotificationName:@"RELOAD_DATA" object:nil]; [self.navigationController popViewControllerAnimated:YES]; } // 注销 -(void)unRegister{ NSString *link= [[NSUserDefaults standardUserDefaults] stringForKey:UNREGISTER_LINK]; if(link&&![link isEqual:@""]) { NSLog(@"注销"); SettingWebView *settingWebViewController = [[SettingWebView alloc] init]; settingWebViewController.webTitle=@"账号注销"; settingWebViewController.requestURL=link; [self.navigationController pushViewController:settingWebViewController animated:YES]; }else{ [self.view makeToast:@"尚未获取到注销链接"]; } } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { } #pragma mark UITableViewDelegate UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if(_userOnLine){ if (section==3||section==4) { return 1; } } return [_dataAry[section] count]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ if (_userOnLine) { return _dataAry.count+2; } return _dataAry.count; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if(section==0){ return 30; }else if(section==3||section==4){ return 0; } return 22; } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if(section==1){ return [NSString stringWithFormat:@"当前空间"]; } return [NSString stringWithFormat:@""]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.section==1&&indexPath.row==0){ //手机存储空间cell StorageSpaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfly1]; cell.text.text=[[_dataAry objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; //将字节装换成GB float total=[[self totalDiskSpace] floatValue]/(1024.0*1024.0*1024.0); cell.total.text=[NSString stringWithFormat:@"%0.2f",total]; //将字节装换成GB float margin=[[self freeDiskSpace] floatValue]/(1024.0*1024.0*1024.0); cell.margin.text=[NSString stringWithFormat:@"%0.2f",margin]; [cell.total sizeToFit]; [cell.margin sizeToFit]; return cell; }else if(indexPath.section==3){ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfly_logout]; if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentfly_logout]; [cell setAccessoryType:UITableViewCellAccessoryNone]; } UIButton *outButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 44)]; [outButton setBackgroundColor:kGlobalBlueColor]; [outButton setTitle:@"退出登录" forState:UIControlStateNormal]; [outButton setTintColor:[UIColor whiteColor]]; [outButton addTarget:self action:@selector(OutOfLogin) forControlEvents:UIControlEventTouchUpInside]; [cell.viewForLastBaselineLayout addSubview:outButton]; return cell; }else if(indexPath.section==4){ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfly_unregister]; if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentfly_unregister]; [cell setAccessoryType:UITableViewCellAccessoryNone]; } UIButton *outButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 44)]; [outButton setBackgroundColor:[UIColor whiteColor]]; // [outButton setBackgroundColor:kGlobalBlueColor]; [outButton setTitle:@"注销账号" forState:UIControlStateNormal]; [outButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [outButton setTintColor:[UIColor grayColor]]; [outButton addTarget:self action:@selector(unRegister) forControlEvents:UIControlEventTouchUpInside]; [cell.viewForLastBaselineLayout addSubview:outButton]; return cell; }else{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfly]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentfly]; [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; } cell.textLabel.text = [[_dataAry objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:NO]; if (indexPath.section==0) { switch (indexPath.row) { case 0:{//清空图片缓存 NSString *str = [NSString stringWithFormat:@"确认清空%.2fM图片缓存!",[self filePath]]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:str preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"点击取消"); }]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self deleteFile]; }]; [alertController addAction:cancelAction]; [alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil]; } break; } } if(indexPath.section==2){ SettingWebView *settingWebViewController = [[SettingWebView alloc] init]; switch (indexPath.row) { case 0:{//常见问题 settingWebViewController.webTitle=@"常见问题"; settingWebViewController.requestURL=changjianwenti; [self.navigationController pushViewController:settingWebViewController animated:YES]; } break; case 1:{//关于我们 settingWebViewController.webTitle=@"关于我们"; settingWebViewController.requestURL=guanyuwomen; [self.navigationController pushViewController:settingWebViewController animated:YES]; } break; case 2:{//免责声明 settingWebViewController.webTitle=@"免责声明"; settingWebViewController.requestURL=mianzeshengming; [self.navigationController pushViewController:settingWebViewController animated:YES]; } break; case 3:{ [UIApplication sharedApplication].keyWindow.userInteractionEnabled = NO; [SVProgressHUD show]; [self performSelector:@selector(popTips) withObject:self afterDelay:2]; } break; default: break; } } if(indexPath.section == 3 && indexPath.row == 0) { [self OutOfLogin]; } if(indexPath.section == 4 && indexPath.row == 0) { [self unRegister]; } } - (void)popTips { [SVProgressHUD dismiss]; [UIApplication sharedApplication].keyWindow.userInteractionEnabled = YES; [self autoDisappearAlertTime:1 msg:@"没有找到可恢复购买项"]; } #pragma mark 删除沙盒里的文件 -(void)deleteFile{ NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ]; NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath]; for ( NSString * p in files) { NSError * error = nil ; NSString * path = [cachPath stringByAppendingPathComponent :p]; if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) { [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error]; } } [_mytableview reloadData]; } - (NSMutableArray *)readDataFromPath:(NSString *)filePath{ NSMutableArray *array = [NSMutableArray arrayWithCapacity:0]; NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:filePath]) { array = [NSMutableArray arrayWithContentsOfFile:filePath]; return array; } return nil; } -(void)back:(id)sender{ [self.navigationController popViewControllerAnimated:YES]; } -( float )filePath{ NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ]; return [ self folderSizeAtPath :cachPath]; } - (float)folderSizeAtPath:( NSString *) folderPath{ NSFileManager * manager = [ NSFileManager defaultManager ]; if (![manager fileExistsAtPath :folderPath]){ return 0 ; } NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ]; NSString * fileName; long long folderSize = 0 ; while ((fileName = [childFilesEnumerator nextObject ]) != nil ){ NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName]; folderSize += [ self fileSizeAtPath :fileAbsolutePath]; } return folderSize/( 1024.0 * 1024.0 ); } - ( long long ) fileSizeAtPath:( NSString *) filePath{ NSFileManager * manager = [ NSFileManager defaultManager ]; if ([manager fileExistsAtPath :filePath]){ return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ]; } return 0 ; } @end