天天省钱快报

KBSettingViewController.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. //
  2. // KBSettingViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/25.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBSettingViewController.h"
  9. #import "KBAuthorityManager.h"
  10. #import "CCActionSheet.h"
  11. #import <AlibabaAuthSDK/ALBBSession.h>
  12. #import <AlibabaAuthSDK/ALBBSDK.h>
  13. #import "DXAlertView.h"
  14. #import "KBLoginViewController.h"
  15. #import <StoreKit/StoreKit.h>
  16. #import "KBMySuperViewController.h"
  17. @interface KBSettingViewController ()<UITableViewDelegate,UITableViewDataSource,CCActionSheetDelegate,DXAlertViewDelegate>
  18. @property (nonatomic, strong) UITableView *tableView;
  19. @property (nonatomic, strong) NSArray *dataArr;
  20. @property (nonatomic, strong) NSArray *sexArr;
  21. @end
  22. @implementation KBSettingViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. [self configNavigationBar];
  26. [self addObserToController];
  27. }
  28. - (void)configNavigationBar {
  29. self.view.backgroundColor = [UIColor whiteColor];
  30. [self.navigationBar setNavTitle:@"设置"];
  31. self.navigationBar.showNavigationBarBottomLine = YES;
  32. [self.view addSubview:self.tableView];
  33. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  34. [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  35. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  36. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  37. UILabel *logOut = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50-BottomMargin, SCREEN_WIDTH, 50)];
  38. logOut.backgroundColor = [UIColor whiteColor];
  39. logOut.textColor = [UIColor homeRedColor];
  40. logOut.textAlignment = NSTextAlignmentCenter;
  41. logOut.font = [UIFont systemFontOfSize:14];
  42. logOut.text = [AccountTool isLogin] ? @"退出登录":@"快速登录";
  43. logOut.userInteractionEnabled = YES;
  44. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logOutAction)];
  45. [logOut addGestureRecognizer:tap];
  46. [self.view addSubview:logOut];}
  47. - (void)addObserToController {
  48. [[NSNotificationCenter defaultCenter] addObserver:self
  49. selector:@selector(becomActive)
  50. name:UIApplicationDidBecomeActiveNotification object:nil];
  51. }
  52. - (void)becomActive {
  53. [self.tableView reloadData];
  54. }
  55. /**
  56. 返回
  57. */
  58. - (void)backAction {
  59. [self.navigationController popViewControllerAnimated:YES];
  60. }
  61. /**
  62. 退出登录
  63. */
  64. - (void)logOutAction {
  65. if ([AccountTool isLogin]) {
  66. // DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"温馨提示" message:@"是否要退出登录?" cancelBtnTitle:@"取消" otherBtnTitle:@"确定"];
  67. // alert.clickBlock = ^(NSInteger index) {
  68. // if (index == 1) {
  69. // [self logOutManager];
  70. // }
  71. // };
  72. // [alert show];
  73. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要退出登录?" preferredStyle:UIAlertControllerStyleAlert];
  74. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  75. }];
  76. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  77. [self logOutManager];
  78. }];
  79. [alert addAction:cancel];
  80. [alert addAction:sure];
  81. [self presentViewController:alert animated:YES completion:nil];
  82. }else {
  83. KBLoginViewController *login = [[KBLoginViewController alloc] init];
  84. [self presentViewController:login animated:YES completion:nil];
  85. }
  86. }
  87. - (void)logOutManager {
  88. [KBHttp post:Logout params:nil success:^(id json) {
  89. [AccountTool deleteAccount];
  90. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  91. [self.navigationController popViewControllerAnimated:YES];
  92. } failure:^(NSError *error) {
  93. }];
  94. }
  95. /**
  96. switch 点击
  97. */
  98. - (void)switchAction {
  99. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  100. if([[UIApplication sharedApplication]canOpenURL:url] ) {
  101. if (@available(iOS 10.0, *)) {
  102. [[UIApplication sharedApplication] openURL:url options:@{}completionHandler:^(BOOL success) {
  103. }];
  104. } else {
  105. [[UIApplication sharedApplication]openURL:url];
  106. }
  107. }
  108. }
  109. #pragma mark ------------------
  110. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  111. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  112. [cell setSeparatorInset:UIEdgeInsetsMake(0, 50, 0, 0)];
  113. }
  114. }
  115. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  116. return self.dataArr.count;
  117. }
  118. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  119. return 50;
  120. }
  121. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  122. return 0.1;
  123. }
  124. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  125. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellID"];
  126. cell.textLabel.text = self.dataArr[indexPath.row][@"title"];
  127. if (indexPath.row != 0 && indexPath.row != 3) {
  128. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  129. }
  130. // if (indexPath.row == 0) {
  131. // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  132. //
  133. // cell.detailTextLabel.text = [sex isEqualToString:@"0"] ? @"女":@"男";
  134. // }
  135. if (indexPath.row == 0) {
  136. UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 10, 50, 30)];
  137. [switchView addTarget:self action:@selector(switchAction) forControlEvents:UIControlEventTouchUpInside]; // 开关事件切换通知
  138. [cell addSubview:switchView];
  139. if ([KBAuthorityManager isObtainUserNotificationAuthority]) {
  140. switchView.on = YES;
  141. }else {
  142. switchView.on = NO;
  143. }
  144. }
  145. if (indexPath.row == 1) {
  146. NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
  147. NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]];
  148. cell.detailTextLabel.text = currentVolum;
  149. }
  150. if (indexPath.row == 2) {
  151. if ([[ALBBSession sharedInstance] isLogin]) {
  152. cell.detailTextLabel.text = @"已授权";
  153. }else {
  154. cell.detailTextLabel.text = @"未授权";
  155. }
  156. }
  157. NSString *imgName = self.dataArr[indexPath.row][@"image"];
  158. cell.imageView.image = [UIImage imageNamed:imgName];
  159. cell.textLabel.font = [UIFont systemFontOfSize:15];
  160. cell.textLabel.textColor = [UIColor YHColorWithHex:0x666666];
  161. cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
  162. cell.detailTextLabel.textColor = [UIColor YHColorWithHex:0x999999];
  163. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  164. if (indexPath.row == 3) {
  165. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  166. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  167. cell.detailTextLabel.text = app_Version;
  168. }
  169. return cell;
  170. }
  171. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  172. // if (indexPath.row == 0) {
  173. // //改变性别
  174. // [self changeUserSex];
  175. // }
  176. if (indexPath.row == 1) {
  177. //清理缓存
  178. [self clearCacheWith:indexPath];
  179. }
  180. if (indexPath.row == 2) {
  181. //淘宝授权
  182. [self taobaoAuthor];
  183. }
  184. if (indexPath.row == 4) {
  185. //五星好评
  186. [self commentAndStart];
  187. }
  188. if (indexPath.row == 5) {
  189. if (![AccountTool isLogin]) {
  190. [self goToLoginPage];
  191. return;
  192. }
  193. KBMySuperViewController *superV = [[KBMySuperViewController alloc] init];
  194. [self.navigationController pushViewController:superV animated:YES];
  195. }
  196. }
  197. #pragma mark ------------ private----------
  198. /**
  199. 五星好评
  200. */
  201. - (void)commentAndStart {
  202. NSString * nsStringToOpen = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",APP_ID];//替换为对应的APPID
  203. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]];
  204. }
  205. /**
  206. 用户登录
  207. */
  208. - (void)goToLoginPage {
  209. KBLoginViewController *login = [[KBLoginViewController alloc] init];
  210. [self presentViewController:login animated:YES completion:nil];
  211. }
  212. /**
  213. 改变性别
  214. */
  215. - (void)changeUserSex {
  216. CCActionSheet *actionSheet = [[CCActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:self.sexArr];
  217. [actionSheet showInView:self.view];
  218. }
  219. /**
  220. 清理缓存
  221. */
  222. - (void)clearCacheWith:(NSIndexPath *)indexPath {
  223. [[SDImageCache sharedImageCache] clearMemory];
  224. [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
  225. NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
  226. NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]];
  227. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  228. cell.detailTextLabel.text = currentVolum;
  229. }];
  230. }
  231. /**
  232. 淘宝授权
  233. */
  234. - (void)taobaoAuthor {
  235. if ([[ALBBSession sharedInstance] isLogin]) {
  236. // DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"温馨提示" message:@"是否取消淘宝授权?" cancelBtnTitle:@"取消" otherBtnTitle:@"确定"];
  237. // alert.delegate = self;
  238. // [alert show];
  239. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要取消淘宝授权?" preferredStyle:UIAlertControllerStyleAlert];
  240. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  241. }];
  242. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  243. ALBBSDK *albbSDK = [ALBBSDK sharedInstance];
  244. [albbSDK logoutWithCallback:^{
  245. [self.tableView reloadData];
  246. }];
  247. }];
  248. [alert addAction:cancel];
  249. [alert addAction:sure];
  250. [self presentViewController:alert animated:YES completion:nil];
  251. }else {
  252. ALBBSDK *albbSDK = [ALBBSDK sharedInstance];
  253. [albbSDK setAppkey:ALBC_APP_KEY];
  254. [albbSDK setAuthOption:NormalAuth];
  255. [albbSDK auth:self successCallback:^(ALBBSession *session){
  256. // ALBBUser *user = [session getUser];
  257. [self.tableView reloadData];
  258. } failureCallback:^(ALBBSession *session,NSError *error){
  259. NSLog(@"session == %@,error == %@",session,error);
  260. }];
  261. }
  262. }
  263. #pragma mark ---------------- DXAlertView delegate ---------
  264. - (void)dxAlertView:(DXAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  265. if (buttonIndex == 1) {
  266. ALBBSDK *albbSDK = [ALBBSDK sharedInstance];
  267. [albbSDK logoutWithCallback:^{
  268. [self.tableView reloadData];
  269. }];
  270. }
  271. }
  272. //计算出大小
  273. - (NSString *)fileSizeWithInterge:(NSInteger)size{
  274. // 1k = 1024, 1m = 1024k
  275. if (size < 1024) {// 小于1k
  276. return [NSString stringWithFormat:@"%ld B",(long)size];
  277. }else if (size < 1024 * 1024){// 小于1m
  278. CGFloat aFloat = size/1024;
  279. return [NSString stringWithFormat:@"%.0fK",aFloat];
  280. }else if (size < 1024 * 1024 * 1024){// 小于1G
  281. CGFloat aFloat = size/(1024 * 1024);
  282. return [NSString stringWithFormat:@"%.1fM",aFloat];
  283. }else{
  284. CGFloat aFloat = size/(1024*1024*1024);
  285. return [NSString stringWithFormat:@"%.1fG",aFloat];
  286. }
  287. }
  288. #pragma mark ------------------- CCActionSheet Delegate ---------
  289. - (void)actionSheet:(CCActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  290. NSDictionary *para = @{@"sex":@(buttonIndex)};
  291. if ([AccountTool isLogin]) {
  292. [KBHttp post:MySetting params:para success:^(id json) {
  293. NSInteger localSex = [[[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey] integerValue];
  294. if (buttonIndex != localSex) {
  295. [self saveSexWithButtonIndex:buttonIndex];
  296. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  297. }
  298. } failure:^(NSError *error) {
  299. }];
  300. }else {
  301. [self saveSexWithButtonIndex:buttonIndex];
  302. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  303. }
  304. }
  305. - (void)saveSexWithButtonIndex:(NSInteger)buttonIndex {
  306. if (buttonIndex == 0) {
  307. [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:UserSexKey];
  308. }else {
  309. [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:UserSexKey];
  310. }
  311. [[NSUserDefaults standardUserDefaults] synchronize];
  312. [self.tableView reloadData];
  313. }
  314. - (UITableView *)tableView {
  315. if (!_tableView) {
  316. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
  317. _tableView.estimatedSectionHeaderHeight = 0;
  318. _tableView.estimatedSectionFooterHeight = 0;
  319. _tableView.sectionFooterHeight = 0;
  320. _tableView.sectionHeaderHeight = 0;
  321. _tableView.delegate = self;
  322. _tableView.dataSource = self;
  323. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  324. _tableView.backgroundColor = [UIColor yhGrayColor];
  325. _tableView.bounces = YES;
  326. _tableView.showsVerticalScrollIndicator = NO;
  327. [_tableView setSeparatorColor:[UIColor YHColorWithHex:0xdddddd]];
  328. }
  329. return _tableView;
  330. }
  331. - (NSArray *)dataArr {
  332. if (!_dataArr) {
  333. _dataArr = @[@{@"title":@"推送设置",@"image":@"push_img"},
  334. @{@"title":@"清理缓存",@"image":@"clear_cache"},
  335. @{@"title":@"淘宝授权",@"image":@"taobao_man"},
  336. @{@"title":@"版本信息",@"image":@"verinfo"},
  337. ];
  338. }
  339. return _dataArr;
  340. }
  341. - (NSArray *)sexArr {
  342. if (!_sexArr) {
  343. _sexArr = @[@"女",@"男"];
  344. }
  345. return _sexArr;
  346. }
  347. - (void)dealloc {
  348. [[NSNotificationCenter defaultCenter] removeObserver:self];
  349. }
  350. - (void)didReceiveMemoryWarning {
  351. [super didReceiveMemoryWarning];
  352. // Dispose of any resources that can be recreated.
  353. }
  354. /*
  355. #pragma mark - Navigation
  356. // In a storyboard-based application, you will often want to do a little preparation before navigation
  357. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  358. // Get the new view controller using [segue destinationViewController].
  359. // Pass the selected object to the new view controller.
  360. }
  361. */
  362. @end