省钱达人

AccountTool.m 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // AccountTool.m
  3. // CommerceManage
  4. //
  5. // Created by 小花 on 2016/12/27.
  6. // Copyright © 2016年 vaic. All rights reserved.
  7. //
  8. #import "AccountTool.h"
  9. #import "MiPushSDK.h"
  10. //账号信息存储路径
  11. #define CCAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"]
  12. @implementation AccountTool
  13. /**
  14. * 存储账号信息
  15. *
  16. * @param account 账号模型
  17. */
  18. + (void)saveAccount:(AccountModel *)account
  19. {
  20. //小米根据token注册别名
  21. [MiPushSDK setAlias:account.token];
  22. //将一个对象写入沙盒 需要用到一个NSKeyedArchiver 自定义对象的存储必须用这个
  23. [NSKeyedArchiver archiveRootObject:account toFile:CCAccountPath];
  24. }
  25. /**
  26. * 返回账号信息
  27. *
  28. * @return 账号模型(如果账号过期,我们会返回nil)
  29. */
  30. + (AccountModel *)account
  31. {
  32. //加载模型
  33. AccountModel *account=[NSKeyedUnarchiver unarchiveObjectWithFile:CCAccountPath];
  34. return account;
  35. }
  36. /**
  37. * 删除账号信息
  38. *
  39. * @return 是否成功
  40. */
  41. + (BOOL)deleteAccount
  42. {
  43. //加载模型
  44. AccountModel *account=[NSKeyedUnarchiver unarchiveObjectWithFile:CCAccountPath];
  45. [MiPushSDK unsetAlias:account.token];
  46. [[NSUserDefaults standardUserDefaults]removeObjectForKey:UserPhone];
  47. [[NSUserDefaults standardUserDefaults]synchronize];
  48. return [[NSFileManager defaultManager] removeItemAtPath:CCAccountPath error:nil];
  49. }
  50. /**
  51. 是否登录
  52. @return 是否登录
  53. */
  54. + (BOOL)isLogin {
  55. AccountModel *model = [self account];
  56. if ([model.token isEqualToString:@""] || model.token == nil) {
  57. return NO;
  58. }else {
  59. return YES;
  60. }
  61. }
  62. @end